-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
TAGS=with_gvisor,with_quic,with_wireguard,with_ech,with_utls,with_clash_api,with_grpc | ||
# TAGS=with_dhcp,with_low_memory,with_conntrack | ||
go run --tags $TAGS ./cmd $@ | ||
go run --tags $TAGS ./cli $@ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package cmd | ||
|
||
import ( | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
|
||
v2 "github.com/hiddify/hiddify-core/v2" | ||
"github.com/sagernet/sing-box/log" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var commandInstance = &cobra.Command{ | ||
Use: "instance", | ||
Short: "instance", | ||
Args: cobra.OnlyValidArgs, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
hiddifySetting := defaultConfigs | ||
if hiddifySettingPath != "" { | ||
hiddifySetting2, err := v2.ReadHiddifyOptionsAt(hiddifySettingPath) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
hiddifySetting = *hiddifySetting2 | ||
} | ||
|
||
instance, err := v2.RunInstanceString(&hiddifySetting, configPath) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer instance.Close() | ||
ping, err := instance.PingAverage("http://cp.cloudflare.com", 4) | ||
if err != nil { | ||
// log.Fatal(err) | ||
} | ||
log.Info("Average Ping to Cloudflare : ", ping, "\n") | ||
|
||
for i := 1; i <= 4; i++ { | ||
ping, err := instance.PingCloudflare() | ||
if err != nil { | ||
log.Warn(i, " Error ", err, "\n") | ||
} else { | ||
log.Info(i, " Ping time: ", ping, " ms\n") | ||
} | ||
} | ||
log.Info("Instance is running on port socks5://127.0.0.1:", instance.ListenPort, "\n") | ||
log.Info("Press Ctrl+C to exit\n") | ||
sigChan := make(chan os.Signal, 1) | ||
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM) | ||
<-sigChan | ||
log.Info("CTRL+C recived-->stopping\n") | ||
instance.Close() | ||
}, | ||
} | ||
|
||
func init() { | ||
mainCommand.AddCommand(commandInstance) | ||
addHConfigFlags(commandInstance) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"math/rand" | ||
"net/http" | ||
"net/netip" | ||
"time" | ||
|
||
"github.com/hiddify/hiddify-core/common" | ||
"github.com/hiddify/hiddify-core/extension_repository/cleanip_scanner" | ||
Check failure on line 13 in cmd/cmd_temp.go GitHub Actions / build (hiddify-cli-linux-386, linux, 386, linux-custom, ubuntu-20.04)
Check failure on line 13 in cmd/cmd_temp.go GitHub Actions / build (hiddify-cli-linux-amd64, linux, amd64, v1, linux-custom, ubuntu-20.04)
Check failure on line 13 in cmd/cmd_temp.go GitHub Actions / build (hiddify-cli-linux-mips64, linux, mips64, linux-custom, ubuntu-20.04)
Check failure on line 13 in cmd/cmd_temp.go GitHub Actions / build (hiddify-cli-linux-mips-softfloat, linux, mips, softfloat, linux-custom, ubuntu-20.04)
Check failure on line 13 in cmd/cmd_temp.go GitHub Actions / build (hiddify-cli-linux-mips64el, linux, mips64le, linux-custom, ubuntu-20.04)
Check failure on line 13 in cmd/cmd_temp.go GitHub Actions / build (hiddify-cli-linux-armv6, linux, arm, 6, linux-custom, ubuntu-20.04)
Check failure on line 13 in cmd/cmd_temp.go GitHub Actions / build (hiddify-cli-linux-mips-hardfloat, linux, mips, hardfloat, linux-custom, ubuntu-20.04)
Check failure on line 13 in cmd/cmd_temp.go GitHub Actions / build (hiddify-cli-linux-mipsel-hardfloat, linux, mipsle, hardfloat, linux-custom, ubuntu-20.04)
Check failure on line 13 in cmd/cmd_temp.go GitHub Actions / build (hiddify-cli-linux-s390x, linux, s390x, linux-custom, ubuntu-20.04)
Check failure on line 13 in cmd/cmd_temp.go GitHub Actions / build (hiddify-cli-linux-mipsel-softfloat, linux, mipsle, softfloat, linux-custom, ubuntu-20.04)
Check failure on line 13 in cmd/cmd_temp.go GitHub Actions / build (hiddify-cli-linux-arm64, linux, arm64, linux-custom, ubuntu-20.04)
Check failure on line 13 in cmd/cmd_temp.go GitHub Actions / build (hiddify-cli-linux-armv5, linux, arm, 5, linux-custom, ubuntu-20.04)
Check failure on line 13 in cmd/cmd_temp.go GitHub Actions / build (hiddify-cli-linux-armv7, linux, arm, 7, linux-custom, ubuntu-20.04)
Check failure on line 13 in cmd/cmd_temp.go GitHub Actions / build (hiddify-core-linux-amd64, ubuntu-20.04, linux-amd64)
Check failure on line 13 in cmd/cmd_temp.go GitHub Actions / build (hiddify-cli-linux-amd64-v3, linux, amd64, v3, linux-custom, ubuntu-20.04)
Check failure on line 13 in cmd/cmd_temp.go GitHub Actions / build (hiddify-core-windows-amd64, ubuntu-latest, windows-amd64, x64)
Check failure on line 13 in cmd/cmd_temp.go GitHub Actions / build (hiddify-core-windows-amd64, ubuntu-latest, windows-amd64, x64)
Check failure on line 13 in cmd/cmd_temp.go GitHub Actions / build (hiddify-core-macos-universal, macos-12, macos-universal)
|
||
"github.com/spf13/cobra" | ||
"golang.org/x/net/proxy" | ||
) | ||
|
||
var commandTemp = &cobra.Command{ | ||
Use: "temp", | ||
Short: "temp", | ||
Args: cobra.MaximumNArgs(2), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
// fmt.Printf("Ping time: %d ms\n", Ping()) | ||
scanner := cleanip_scanner.NewScannerEngine(&cleanip_scanner.ScannerOptions{ | ||
UseIPv4: true, | ||
UseIPv6: common.CanConnectIPv6(), | ||
MaxDesirableRTT: 500 * time.Millisecond, | ||
IPQueueSize: 4, | ||
IPQueueTTL: 10 * time.Second, | ||
ConcurrentPings: 10, | ||
// MaxDesirableIPs: e.count, | ||
CidrList: cleanip_scanner.DefaultCFRanges(), | ||
PingFunc: func(ip netip.Addr) (cleanip_scanner.IPInfo, error) { | ||
fmt.Printf("Ping: %s\n", ip.String()) | ||
return cleanip_scanner.IPInfo{ | ||
AddrPort: netip.AddrPortFrom(ip, 80), | ||
RTT: time.Duration(rand.Intn(1000)), | ||
CreatedAt: time.Now(), | ||
}, nil | ||
}, | ||
}, | ||
) | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) | ||
defer cancel() | ||
|
||
scanner.Run(ctx) | ||
|
||
t := time.NewTicker(1 * time.Second) | ||
defer t.Stop() | ||
|
||
for { | ||
ipList := scanner.GetAvailableIPs(false) | ||
if len(ipList) > 1 { | ||
// e.result = "" | ||
for i := 0; i < 2; i++ { | ||
// result = append(result, ipList[i]) | ||
// e.result = e.result + ipList[i].AddrPort.String() + "\n" | ||
fmt.Printf("%d %s\n", ipList[i].RTT, ipList[i].AddrPort.String()) | ||
} | ||
return | ||
} | ||
|
||
select { | ||
case <-ctx.Done(): | ||
// Context is done | ||
return | ||
case <-t.C: | ||
// Prevent the loop from spinning too fast | ||
continue | ||
} | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
mainCommand.AddCommand(commandTemp) | ||
} | ||
|
||
func GetContent(url string) (string, error) { | ||
return ContentFromURL("GET", url, 10*time.Second) | ||
} | ||
|
||
func ContentFromURL(method string, url string, timeout time.Duration) (string, error) { | ||
if method == "" { | ||
return "", fmt.Errorf("empty method") | ||
} | ||
if url == "" { | ||
return "", fmt.Errorf("empty url") | ||
} | ||
|
||
req, err := http.NewRequest(method, url, nil) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
dialer, err := proxy.SOCKS5("tcp", "127.0.0.1:12334", nil, proxy.Direct) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
transport := &http.Transport{ | ||
Dial: dialer.Dial, | ||
} | ||
|
||
client := &http.Client{ | ||
Transport: transport, | ||
Timeout: timeout, | ||
} | ||
|
||
resp, err := client.Do(req) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent { | ||
return "", fmt.Errorf("request failed with status code: %d", resp.StatusCode) | ||
} | ||
|
||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if body == nil { | ||
return "", fmt.Errorf("empty body") | ||
} | ||
|
||
return string(body), nil | ||
} | ||
|
||
func Ping() int { | ||
startTime := time.Now() | ||
_, err := ContentFromURL("HEAD", "https://cp.cloudflare.com", 4*time.Second) | ||
if err != nil { | ||
return -1 | ||
} | ||
duration := time.Since(startTime) | ||
return int(duration.Milliseconds()) | ||
} |