mirror of
https://github.com/SajadMRjl/find-me-internet.git
synced 2026-07-19 08:46:26 +00:00
fix: add config and logger, cleanup
This commit is contained in:
36
internal/config/config.go
Normal file
36
internal/config/config.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/kelseyhightower/envconfig"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
// App Settings
|
||||
LogLevel string `envconfig:"LOG_LEVEL" default:"INFO"` // INFO, DEBUG, ERROR
|
||||
Workers int `envconfig:"MAX_WORKERS" default:"10"`
|
||||
|
||||
// Paths
|
||||
SingBoxPath string `envconfig:"SING_BOX_PATH" default:"./bin/sing-box"`
|
||||
|
||||
// Testing Parameters
|
||||
TestURL string `envconfig:"TEST_URL" default:"http://cp.cloudflare.com"`
|
||||
TcpTimeout time.Duration `envconfig:"TCP_TIMEOUT" default:"2s"`
|
||||
TestTimeout time.Duration `envconfig:"TEST_TIMEOUT" default:"10s"`
|
||||
}
|
||||
|
||||
// Load reads .env and maps variables to Config struct
|
||||
func Load() *Config {
|
||||
// 1. Try loading .env file (optional, for local dev)
|
||||
_ = godotenv.Load()
|
||||
|
||||
var cfg Config
|
||||
// 2. Process environment variables
|
||||
if err := envconfig.Process("", &cfg); err != nil {
|
||||
log.Fatalf("Failed to load config: %v", err)
|
||||
}
|
||||
return &cfg
|
||||
}
|
||||
@@ -2,14 +2,15 @@ package filter
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"log/slog"
|
||||
"net"
|
||||
"time"
|
||||
"strconv"
|
||||
|
||||
"time"
|
||||
|
||||
"find-me-internet/internal/model"
|
||||
)
|
||||
|
||||
// Pipeline represents the filter configuration
|
||||
// Pipeline manages the "Cheap Check" logic
|
||||
type Pipeline struct {
|
||||
Timeout time.Duration
|
||||
}
|
||||
@@ -18,29 +19,29 @@ func NewPipeline(timeout time.Duration) *Pipeline {
|
||||
return &Pipeline{Timeout: timeout}
|
||||
}
|
||||
|
||||
// Check performs the TCP & TLS checks
|
||||
// Returns true if the proxy is worth testing with Sing-box
|
||||
// Check runs a sequence of low-cost network tests.
|
||||
// Returns false immediately if any stage fails.
|
||||
func (f *Pipeline) Check(p *model.Proxy) bool {
|
||||
// 1. Syntax Check
|
||||
if p.Address == "" || p.Port == 0 || p.Type == model.TypeUnknown {
|
||||
// 1. Sanity Check
|
||||
if p.Address == "" || p.Port == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
// 2. TCP Connect (Liveness)
|
||||
// 2. TCP Liveness (Is the port open?)
|
||||
if !f.checkTCP(p) {
|
||||
slog.Debug("TCP Connection failed", "addr", p.Address, "port", p.Port)
|
||||
p.IsOnline = false
|
||||
return false // Dead
|
||||
return false
|
||||
}
|
||||
p.IsOnline = true
|
||||
|
||||
// 3. TLS Handshake (Validity)
|
||||
// Only run this if the proxy uses TLS (SNI is present or it's a TLS protocol)
|
||||
// For VLESS/Trojan, TLS is standard. For VMess, it's optional.
|
||||
// 3. TLS Validity (Does it handshake?)
|
||||
// Only required if SNI is present or port is standard HTTPS
|
||||
if p.SNI != "" || p.Port == 443 {
|
||||
if !f.checkTLS(p) {
|
||||
slog.Debug("TLS Handshake failed", "addr", p.Address, "sni", p.SNI)
|
||||
p.IsTLSSecure = false
|
||||
// If it expects TLS but fails handshake, it's likely a firewall block or dead cert
|
||||
return false
|
||||
return false
|
||||
}
|
||||
p.IsTLSSecure = true
|
||||
}
|
||||
@@ -48,7 +49,6 @@ func (f *Pipeline) Check(p *model.Proxy) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// checkTCP attempts a raw socket connection
|
||||
func (f *Pipeline) checkTCP(p *model.Proxy) bool {
|
||||
address := net.JoinHostPort(p.Address, strconv.Itoa(p.Port))
|
||||
conn, err := net.DialTimeout("tcp", address, f.Timeout)
|
||||
@@ -59,20 +59,18 @@ func (f *Pipeline) checkTCP(p *model.Proxy) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// checkTLS attempts a TLS handshake
|
||||
func (f *Pipeline) checkTLS(p *model.Proxy) bool {
|
||||
address := net.JoinHostPort(p.Address, strconv.Itoa(p.Port))
|
||||
|
||||
dialer := &net.Dialer{Timeout: f.Timeout}
|
||||
|
||||
// We use InsecureSkipVerify because many proxies use self-signed certs or Reality.
|
||||
// We only care that the server *speaks* TLS and accepts our SNI.
|
||||
// We skip verification because many proxies use self-signed certs or Reality.
|
||||
// The goal is to check if the server *speaks* TLS, not if the cert is trusted by Root CAs.
|
||||
conf := &tls.Config{
|
||||
InsecureSkipVerify: true,
|
||||
ServerName: p.SNI,
|
||||
ServerName: p.SNI,
|
||||
}
|
||||
|
||||
// If no SNI is parsed, try the host address (common for direct connections)
|
||||
// Fallback SNI if none provided
|
||||
if conf.ServerName == "" {
|
||||
conf.ServerName = p.Address
|
||||
}
|
||||
|
||||
31
internal/logger/logger.go
Normal file
31
internal/logger/logger.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Setup initializes the global logger based on config
|
||||
func Setup(level string) {
|
||||
var logLevel slog.Level
|
||||
|
||||
switch strings.ToUpper(level) {
|
||||
case "DEBUG":
|
||||
logLevel = slog.LevelDebug
|
||||
case "WARN":
|
||||
logLevel = slog.LevelWarn
|
||||
case "ERROR":
|
||||
logLevel = slog.LevelError
|
||||
default:
|
||||
logLevel = slog.LevelInfo
|
||||
}
|
||||
|
||||
// Create a structured text handler (easier to read than JSON in terminal)
|
||||
handler := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
|
||||
Level: logLevel,
|
||||
})
|
||||
|
||||
logger := slog.New(handler)
|
||||
slog.SetDefault(logger)
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package parser
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"strings"
|
||||
|
||||
"find-me-internet/internal/model"
|
||||
@@ -10,11 +11,13 @@ import (
|
||||
"github.com/gvcgo/vpnparser/pkgs/outbound"
|
||||
)
|
||||
|
||||
// tempConfig allows us to extract deep fields from the Sing-box JSON
|
||||
// tempConfig covers multiple locations where SNI might be hidden in the JSON
|
||||
type tempConfig struct {
|
||||
Transport struct {
|
||||
Type string `json:"type"`
|
||||
} `json:"transport"`
|
||||
|
||||
// Standard TLS
|
||||
TLS struct {
|
||||
ServerName string `json:"server_name"`
|
||||
} `json:"tls"`
|
||||
@@ -26,22 +29,42 @@ func ParseLink(raw string) (*model.Proxy, error) {
|
||||
return nil, fmt.Errorf("empty link")
|
||||
}
|
||||
|
||||
// 1. Parse Raw Link
|
||||
// We omit the second argument to let the library use default parsing
|
||||
// 1. Parse using library
|
||||
item := outbound.ParseRawUriToProxyItem(raw)
|
||||
if item == nil {
|
||||
return nil, fmt.Errorf("unknown protocol or invalid link")
|
||||
return nil, fmt.Errorf("invalid link")
|
||||
}
|
||||
|
||||
// 2. Initialize Proxy Model
|
||||
p := &model.Proxy{
|
||||
RawLink: raw,
|
||||
Address: item.Address,
|
||||
Port: item.Port,
|
||||
}
|
||||
|
||||
// 3. Extract SNI and Network from the Outbound JSON
|
||||
// The library packs the details into 'item.Outbound' string
|
||||
// 2. Clean up Protocol (Fixing the "vless://" bug)
|
||||
// Some versions of the lib return "vless://" instead of "vless"
|
||||
scheme := strings.ToLower(item.Scheme)
|
||||
scheme = strings.TrimSuffix(scheme, "://")
|
||||
|
||||
switch scheme {
|
||||
case "vless":
|
||||
p.Type = model.TypeVLESS
|
||||
if strings.Contains(raw, "reality") {
|
||||
p.Type = model.TypeVLESS
|
||||
}
|
||||
case "vmess":
|
||||
p.Type = model.TypeVMess
|
||||
case "trojan":
|
||||
p.Type = model.TypeTrojan
|
||||
case "ss", "shadowsocks":
|
||||
p.Type = model.TypeShadowsocks
|
||||
default:
|
||||
p.Type = model.TypeUnknown
|
||||
slog.Warn("Unknown protocol", "scheme", scheme, "raw", item.Scheme)
|
||||
}
|
||||
|
||||
// 3. Extract SNI (Fixing the "sni=" bug)
|
||||
// We first try to get it from the standard fields
|
||||
if item.Outbound != "" {
|
||||
var cfg tempConfig
|
||||
if err := json.Unmarshal([]byte(item.Outbound), &cfg); err == nil {
|
||||
@@ -49,26 +72,48 @@ func ParseLink(raw string) (*model.Proxy, error) {
|
||||
p.SNI = cfg.TLS.ServerName
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback: If JSON extraction failed but we have a generic "Host" (sometimes used as SNI)
|
||||
// Note: 'item.Host' doesn't exist either, so we rely solely on JSON extraction above.
|
||||
|
||||
// 4. Map Protocol (Library uses 'Scheme')
|
||||
switch strings.ToLower(item.Scheme) {
|
||||
case "vless":
|
||||
p.Type = model.TypeVLESS
|
||||
if strings.Contains(raw, "reality") {
|
||||
p.Type = model.TypeVLESS // Reality is technically VLESS
|
||||
// 4. Fallback for SNI (Crucial for Reality/VLESS)
|
||||
// If JSON extraction failed, try to parse the raw URL query parameters manually.
|
||||
// This is often more reliable than the JSON dump for simple fields.
|
||||
if p.SNI == "" {
|
||||
// Quick and dirty manual check for "&sni=..." or "&peer=..."
|
||||
if val := extractQueryParam(raw, "sni"); val != "" {
|
||||
p.SNI = val
|
||||
} else if val := extractQueryParam(raw, "peer"); val != "" {
|
||||
p.SNI = val // "peer" is often used in Telegram proxies as SNI
|
||||
} else if val := extractQueryParam(raw, "host"); val != "" {
|
||||
p.SNI = val
|
||||
}
|
||||
case "vmess":
|
||||
p.Type = model.TypeVMess
|
||||
case "trojan":
|
||||
p.Type = model.TypeTrojan
|
||||
case "shadowsocks", "ss":
|
||||
p.Type = model.TypeShadowsocks
|
||||
default:
|
||||
p.Type = model.TypeUnknown
|
||||
}
|
||||
|
||||
// 5. Final Safety: Reality MUST have an SNI
|
||||
// If we still don't have one, the TLS check will inevitably fail.
|
||||
// We can warn here.
|
||||
if p.Type == model.TypeVLESS && p.SNI == "" {
|
||||
slog.Debug("Warning: VLESS proxy has no SNI", "addr", p.Address)
|
||||
}
|
||||
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// Helper to manually grab query params from the raw string
|
||||
// because sometimes the parser library logic is opaque.
|
||||
func extractQueryParam(url, key string) string {
|
||||
// Find "key="
|
||||
keyStr := key + "="
|
||||
start := strings.Index(url, keyStr)
|
||||
if start == -1 {
|
||||
return ""
|
||||
}
|
||||
// Move to value start
|
||||
start += len(keyStr)
|
||||
|
||||
// Find end of value (either '&' or '#')
|
||||
rest := url[start:]
|
||||
end := strings.IndexAny(rest, "&#")
|
||||
if end == -1 {
|
||||
return rest
|
||||
}
|
||||
return rest[:end]
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package tester
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
@@ -14,14 +15,13 @@ import (
|
||||
"find-me-internet/internal/model"
|
||||
)
|
||||
|
||||
// Runner handles the execution of Sing-box
|
||||
type Runner struct {
|
||||
BinPath string
|
||||
TestURL string
|
||||
Timeout time.Duration
|
||||
}
|
||||
|
||||
func NewRunner(binPath string, testURL string, timeout time.Duration) *Runner {
|
||||
func NewRunner(binPath, testURL string, timeout time.Duration) *Runner {
|
||||
return &Runner{
|
||||
BinPath: binPath,
|
||||
TestURL: testURL,
|
||||
@@ -29,73 +29,67 @@ func NewRunner(binPath string, testURL string, timeout time.Duration) *Runner {
|
||||
}
|
||||
}
|
||||
|
||||
// Test performs the full latency check
|
||||
// Test spins up a Sing-box instance and measures HTTP latency
|
||||
func (r *Runner) Test(p *model.Proxy) error {
|
||||
// 1. Get a random free port
|
||||
// 1. Acquire Local Port
|
||||
port, err := getFreePort()
|
||||
if err != nil {
|
||||
return fmt.Errorf("no free ports: %v", err)
|
||||
return fmt.Errorf("failed to get port: %w", err)
|
||||
}
|
||||
|
||||
// 2. Generate Config
|
||||
// 2. Generate Configuration
|
||||
configData, err := GenerateConfig(p, port)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 3. Write Config to Temp File
|
||||
// specific name helps debugging if needed: config_<port>.json
|
||||
configName := filepath.Join(os.TempDir(), fmt.Sprintf("sb_config_%d.json", port))
|
||||
// 3. Write Config File
|
||||
// Using a unique name prevents collisions in concurrent tests
|
||||
configName := filepath.Join(os.TempDir(), fmt.Sprintf("sb_%d_%s.json", port, p.Address))
|
||||
if err := os.WriteFile(configName, configData, 0644); err != nil {
|
||||
return err
|
||||
}
|
||||
defer os.Remove(configName) // Cleanup after test
|
||||
defer os.Remove(configName) // Cleanup
|
||||
|
||||
// 4. Start Sing-box Process
|
||||
// 4. Execute Sing-box
|
||||
ctx, cancel := context.WithTimeout(context.Background(), r.Timeout+2*time.Second)
|
||||
defer cancel()
|
||||
|
||||
cmd := exec.CommandContext(ctx, r.BinPath, "run", "-c", configName)
|
||||
// cmd.Stdout = os.Stdout // Uncomment for debugging
|
||||
// cmd.Stderr = os.Stderr
|
||||
|
||||
if err := cmd.Start(); err != nil {
|
||||
return fmt.Errorf("failed to start sing-box: %v", err)
|
||||
return fmt.Errorf("startup failed: %w", err)
|
||||
}
|
||||
|
||||
// Ensure process is killed when function exits
|
||||
|
||||
// Ensure cleanup happens even if panic occurs
|
||||
defer func() {
|
||||
if cmd.Process != nil {
|
||||
cmd.Process.Kill()
|
||||
_ = cmd.Process.Kill()
|
||||
}
|
||||
}()
|
||||
|
||||
// 5. Wait for Sing-box to initialize
|
||||
// A smart retry loop is better than a fixed sleep
|
||||
proxyReady := waitForPort(port, 2*time.Second)
|
||||
if !proxyReady {
|
||||
return fmt.Errorf("sing-box did not start in time")
|
||||
// 5. Wait for Binding
|
||||
if !waitForPort(port, 2*time.Second) {
|
||||
return fmt.Errorf("sing-box failed to bind port %d", port)
|
||||
}
|
||||
|
||||
// 6. Perform HTTP Latency Test
|
||||
// 6. HTTP Latency Test
|
||||
latency, err := r.measureLatency(port)
|
||||
if err != nil {
|
||||
slog.Debug("Latency test failed", "err", err, "proxy", p.Address)
|
||||
return err
|
||||
}
|
||||
|
||||
// 7. Success! Update the model
|
||||
p.Latency = latency
|
||||
return nil
|
||||
}
|
||||
|
||||
// measureLatency makes the actual HTTP request
|
||||
func (r *Runner) measureLatency(port int) (time.Duration, error) {
|
||||
proxyUrl, _ := url.Parse(fmt.Sprintf("http://127.0.0.1:%d", port))
|
||||
|
||||
client := &http.Client{
|
||||
Transport: &http.Transport{
|
||||
Proxy: http.ProxyURL(proxyUrl),
|
||||
DisableKeepAlives: true,
|
||||
DisableKeepAlives: true, // Force new connection
|
||||
},
|
||||
Timeout: r.Timeout,
|
||||
}
|
||||
@@ -107,15 +101,14 @@ func (r *Runner) measureLatency(port int) (time.Duration, error) {
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// Check for valid response codes (200 OK or 204 No Content)
|
||||
if resp.StatusCode != 200 && resp.StatusCode != 204 {
|
||||
return 0, fmt.Errorf("bad status code: %d", resp.StatusCode)
|
||||
return 0, fmt.Errorf("invalid status code: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
return time.Since(start), nil
|
||||
}
|
||||
|
||||
// Helpers
|
||||
// getFreePort asks the kernel for a random open port
|
||||
func getFreePort() (int, error) {
|
||||
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user