mirror of
https://github.com/SajadMRjl/find-me-internet.git
synced 2026-07-02 15:09:00 +00:00
feat: add all internals and cmd
This commit is contained in:
@@ -2,9 +2,11 @@ package filter
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"find-me-internet/internal/model"
|
||||
"net"
|
||||
"time"
|
||||
"strconv"
|
||||
|
||||
"find-me-internet/internal/model"
|
||||
)
|
||||
|
||||
// Pipeline represents the filter configuration
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"find-me-internet/internal/model"
|
||||
@@ -11,44 +10,61 @@ import (
|
||||
"github.com/gvcgo/vpnparser/pkgs/outbound"
|
||||
)
|
||||
|
||||
// ParseLink converts a raw proxy string (vless://, vmess://) into our internal Model
|
||||
// tempConfig allows us to extract deep fields from the Sing-box JSON
|
||||
type tempConfig struct {
|
||||
Transport struct {
|
||||
Type string `json:"type"`
|
||||
} `json:"transport"`
|
||||
TLS struct {
|
||||
ServerName string `json:"server_name"`
|
||||
} `json:"tls"`
|
||||
}
|
||||
|
||||
func ParseLink(raw string) (*model.Proxy, error) {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
return nil, fmt.Errorf("empty link")
|
||||
}
|
||||
|
||||
// 1. Use vpnparser to decode the link
|
||||
item, err := outbound.ParseRawUriToProxyItem(raw, outbound.ClientTypeSingBox)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse failed: %w", err)
|
||||
}
|
||||
// 1. Parse Raw Link
|
||||
// We omit the second argument to let the library use default parsing
|
||||
item := outbound.ParseRawUriToProxyItem(raw)
|
||||
if item == nil {
|
||||
return nil, fmt.Errorf("unknown protocol or invalid link")
|
||||
}
|
||||
|
||||
// 2. Map to our Internal Model
|
||||
// The library returns a ProxyItem struct. We extract what we need for the "Cheap Checks".
|
||||
// 2. Initialize Proxy Model
|
||||
p := &model.Proxy{
|
||||
RawLink: raw,
|
||||
Address: item.Address,
|
||||
Port: item.Port,
|
||||
Network: item.Network,
|
||||
SNI: item.Sni,
|
||||
}
|
||||
|
||||
// 3. Determine Protocol (The library stores this in Protocol field)
|
||||
switch strings.ToLower(item.Protocol) {
|
||||
// 3. Extract SNI and Network from the Outbound JSON
|
||||
// The library packs the details into 'item.Outbound' string
|
||||
if item.Outbound != "" {
|
||||
var cfg tempConfig
|
||||
if err := json.Unmarshal([]byte(item.Outbound), &cfg); err == nil {
|
||||
p.Network = cfg.Transport.Type
|
||||
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 // We treat Reality as VLESS with special TLS options
|
||||
p.Type = model.TypeVLESS // Reality is technically VLESS
|
||||
}
|
||||
case "vmess":
|
||||
p.Type = model.TypeVMess
|
||||
case "trojan":
|
||||
p.Type = model.TypeTrojan
|
||||
case "shadowsocks":
|
||||
case "shadowsocks", "ss":
|
||||
p.Type = model.TypeShadowsocks
|
||||
default:
|
||||
p.Type = model.TypeUnknown
|
||||
|
||||
70
internal/tester/config.go
Normal file
70
internal/tester/config.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package tester
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"find-me-internet/internal/model"
|
||||
|
||||
"github.com/gvcgo/vpnparser/pkgs/outbound"
|
||||
)
|
||||
|
||||
// SingBoxConfig is the minimal structure Sing-box expects
|
||||
type SingBoxConfig struct {
|
||||
Log LogConfig `json:"log"`
|
||||
Inbounds []InboundConfig `json:"inbounds"`
|
||||
Outbounds []interface{} `json:"outbounds"` // Interface because structure varies
|
||||
}
|
||||
|
||||
type LogConfig struct {
|
||||
Level string `json:"level"`
|
||||
Output string `json:"output,omitempty"`
|
||||
Disabled bool `json:"disabled"`
|
||||
}
|
||||
|
||||
type InboundConfig struct {
|
||||
Type string `json:"type"`
|
||||
Tag string `json:"tag"`
|
||||
Listen string `json:"listen"`
|
||||
ListenPort int `json:"listen_port"`
|
||||
}
|
||||
|
||||
// GenerateConfig creates a JSON config string for Sing-box
|
||||
func GenerateConfig(p *model.Proxy, localPort int) ([]byte, error) {
|
||||
// 1. Convert the Raw Link directly to Sing-box Outbound Object
|
||||
item := outbound.ParseRawUriToProxyItem(p.RawLink, outbound.SingBox)
|
||||
if item == nil {
|
||||
return nil, fmt.Errorf("failed to parse link for config generation")
|
||||
}
|
||||
|
||||
outboundJsonStr := item.GetOutbound()
|
||||
var sbOutbound interface{}
|
||||
if err := json.Unmarshal([]byte(outboundJsonStr), &sbOutbound); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse sing-box outbound json: %w", err)
|
||||
}
|
||||
|
||||
// 2. Wrap it in the full config structure
|
||||
config := SingBoxConfig{
|
||||
Log: LogConfig{
|
||||
Level: "panic", // Silence all logs to keep console clean
|
||||
Disabled: true,
|
||||
},
|
||||
Inbounds: []InboundConfig{
|
||||
{
|
||||
Type: "mixed", // Supports both SOCKS5 and HTTP
|
||||
Tag: "in-local",
|
||||
Listen: "127.0.0.1",
|
||||
ListenPort: localPort,
|
||||
},
|
||||
},
|
||||
Outbounds: []interface{}{
|
||||
sbOutbound, // The Proxy being tested
|
||||
map[string]string{
|
||||
"type": "direct",
|
||||
"tag": "direct",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
return json.MarshalIndent(config, "", " ")
|
||||
}
|
||||
143
internal/tester/runner.go
Normal file
143
internal/tester/runner.go
Normal file
@@ -0,0 +1,143 @@
|
||||
package tester
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"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 {
|
||||
return &Runner{
|
||||
BinPath: binPath,
|
||||
TestURL: testURL,
|
||||
Timeout: timeout,
|
||||
}
|
||||
}
|
||||
|
||||
// Test performs the full latency check
|
||||
func (r *Runner) Test(p *model.Proxy) error {
|
||||
// 1. Get a random free port
|
||||
port, err := getFreePort()
|
||||
if err != nil {
|
||||
return fmt.Errorf("no free ports: %v", err)
|
||||
}
|
||||
|
||||
// 2. Generate Config
|
||||
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))
|
||||
if err := os.WriteFile(configName, configData, 0644); err != nil {
|
||||
return err
|
||||
}
|
||||
defer os.Remove(configName) // Cleanup after test
|
||||
|
||||
// 4. Start Sing-box Process
|
||||
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)
|
||||
}
|
||||
|
||||
// Ensure process is killed when function exits
|
||||
defer func() {
|
||||
if cmd.Process != nil {
|
||||
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")
|
||||
}
|
||||
|
||||
// 6. Perform HTTP Latency Test
|
||||
latency, err := r.measureLatency(port)
|
||||
if err != nil {
|
||||
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,
|
||||
},
|
||||
Timeout: r.Timeout,
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
resp, err := client.Get(r.TestURL)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
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 time.Since(start), nil
|
||||
}
|
||||
|
||||
// Helpers
|
||||
func getFreePort() (int, error) {
|
||||
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
l, err := net.ListenTCP("tcp", addr)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer l.Close()
|
||||
return l.Addr().(*net.TCPAddr).Port, nil
|
||||
}
|
||||
|
||||
func waitForPort(port int, timeout time.Duration) bool {
|
||||
deadline := time.Now().Add(timeout)
|
||||
for time.Now().Before(deadline) {
|
||||
conn, err := net.DialTimeout("tcp", fmt.Sprintf("127.0.0.1:%d", port), 100*time.Millisecond)
|
||||
if err == nil {
|
||||
conn.Close()
|
||||
return true
|
||||
}
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
}
|
||||
return false
|
||||
}
|
||||
Reference in New Issue
Block a user