Add default probe prompts and SSRF validation for channel monitor targets, with focused tests for blocked internal addresses.
71 lines
2.1 KiB
Go
71 lines
2.1 KiB
Go
package service
|
|
|
|
import (
|
|
"math/rand"
|
|
"strings"
|
|
|
|
"github.com/QuantumNous/new-api/common"
|
|
)
|
|
|
|
var DefaultChannelMonitorRequestTemplates = []string{
|
|
"Explain what an API is in one sentence.",
|
|
"Write a Python function to check if a number is prime.",
|
|
"What is 15 percent of 200? Return only the number.",
|
|
"Translate 'Good morning' into Spanish.",
|
|
"What is the capital of Japan? Answer in one word.",
|
|
"Explain the difference between HTTP and HTTPS briefly.",
|
|
"Write a bash command to count lines in a file.",
|
|
"What does CPU stand for?",
|
|
"Convert 100 kilometers to miles. Return only the number.",
|
|
"Name three primary colors.",
|
|
"Write a SQL query to select all users from a users table.",
|
|
"What is the chemical symbol for water?",
|
|
"Explain gravity in one sentence.",
|
|
"What is 2 to the power of 10? Return only the number.",
|
|
"Explain recursion in one sentence.",
|
|
"Write a Linux command to list files in a directory.",
|
|
"What year did World War II end? Return only the year.",
|
|
"Explain what DNS does in one sentence.",
|
|
"What is the square root of 144? Return only the number.",
|
|
"Summarize photosynthesis in one sentence.",
|
|
}
|
|
|
|
func ParseChannelMonitorRequestTemplates(raw string) []string {
|
|
raw = strings.TrimSpace(raw)
|
|
if raw == "" {
|
|
return cloneDefaultChannelMonitorRequestTemplates()
|
|
}
|
|
|
|
var templates []string
|
|
if err := common.Unmarshal([]byte(raw), &templates); err != nil {
|
|
return cloneDefaultChannelMonitorRequestTemplates()
|
|
}
|
|
|
|
cleaned := make([]string, 0, len(templates))
|
|
for _, template := range templates {
|
|
template = strings.TrimSpace(template)
|
|
if template != "" {
|
|
cleaned = append(cleaned, template)
|
|
}
|
|
}
|
|
if len(cleaned) == 0 {
|
|
return cloneDefaultChannelMonitorRequestTemplates()
|
|
}
|
|
return cleaned
|
|
}
|
|
|
|
func SelectChannelMonitorRequestTemplate(raw string, rng *rand.Rand) string {
|
|
templates := ParseChannelMonitorRequestTemplates(raw)
|
|
if len(templates) == 0 {
|
|
return ""
|
|
}
|
|
if rng != nil {
|
|
return templates[rng.Intn(len(templates))]
|
|
}
|
|
return templates[rand.Intn(len(templates))]
|
|
}
|
|
|
|
func cloneDefaultChannelMonitorRequestTemplates() []string {
|
|
return append([]string(nil), DefaultChannelMonitorRequestTemplates...)
|
|
}
|