Skip to content

Commit

Permalink
new: Big Change, Add support for Extensions 😍
Browse files Browse the repository at this point in the history
  • Loading branch information
x committed Sep 28, 2024
1 parent 1f485e1 commit 6d58012
Show file tree
Hide file tree
Showing 44 changed files with 9,091 additions and 764 deletions.
18 changes: 18 additions & 0 deletions cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,24 @@ import (
"github.com/hiddify/hiddify-core/cmd"
)

type UpdateRequest struct {
Description string `json:"description,omitempty"`
PrivatePods bool `json:"private_pods"`
OperatingMode string `json:"operating_mode,omitempty"`
ActivationState string `json:"activation_state,omitempty"`
}

func main() {
cmd.ParseCli(os.Args[1:])

// var request UpdateRequest
// // jsonTag, err2 := validation.ErrorFieldName(&request, &request.OperatingMode)
// jsonTag, err2 := request.ValName(&request.OperatingMode)

// fmt.Println(jsonTag, err2)
// RegisterExtension("com.example.extension", NewExampleExtension())
// ex := extensionsMap["com.example.extension"].(*Extension[struct])
// fmt.Println(NewExampleExtension().Get())

// fmt.Println(ex.Get())
}
2 changes: 1 addition & 1 deletion cmd.sh
100644 → 100755
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 $@
59 changes: 59 additions & 0 deletions cmd/cmd_instance.go
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)
}
1 change: 0 additions & 1 deletion cmd/cmd_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,5 @@ func init() {
}

func runCommand(cmd *cobra.Command, args []string) {

v2.RunStandalone(hiddifySettingPath, configPath, defaultConfigs)
}
141 changes: 141 additions & 0 deletions cmd/cmd_temp.go
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

View workflow job for this annotation

GitHub Actions / build (hiddify-cli-linux-amd64-v3, linux, amd64, v3, linux-custom, ubuntu-20.04)

no required module provides package github.com/hiddify/hiddify-core/extension_repository/cleanip_scanner; to add it:

Check failure on line 13 in cmd/cmd_temp.go

View workflow job for this annotation

GitHub Actions / build (hiddify-cli-linux-mips64, linux, mips64, linux-custom, ubuntu-20.04)

no required module provides package github.com/hiddify/hiddify-core/extension_repository/cleanip_scanner; to add it:

Check failure on line 13 in cmd/cmd_temp.go

View workflow job for this annotation

GitHub Actions / build (hiddify-cli-linux-armv6, linux, arm, 6, linux-custom, ubuntu-20.04)

no required module provides package github.com/hiddify/hiddify-core/extension_repository/cleanip_scanner; to add it:

Check failure on line 13 in cmd/cmd_temp.go

View workflow job for this annotation

GitHub Actions / build (hiddify-cli-linux-mips-hardfloat, linux, mips, hardfloat, linux-custom, ubuntu-20.04)

no required module provides package github.com/hiddify/hiddify-core/extension_repository/cleanip_scanner; to add it:

Check failure on line 13 in cmd/cmd_temp.go

View workflow job for this annotation

GitHub Actions / build (hiddify-cli-linux-armv5, linux, arm, 5, linux-custom, ubuntu-20.04)

no required module provides package github.com/hiddify/hiddify-core/extension_repository/cleanip_scanner; to add it:

Check failure on line 13 in cmd/cmd_temp.go

View workflow job for this annotation

GitHub Actions / build (hiddify-cli-linux-386, linux, 386, linux-custom, ubuntu-20.04)

no required module provides package github.com/hiddify/hiddify-core/extension_repository/cleanip_scanner; to add it:

Check failure on line 13 in cmd/cmd_temp.go

View workflow job for this annotation

GitHub Actions / build (hiddify-cli-linux-armv7, linux, arm, 7, linux-custom, ubuntu-20.04)

no required module provides package github.com/hiddify/hiddify-core/extension_repository/cleanip_scanner; to add it:

Check failure on line 13 in cmd/cmd_temp.go

View workflow job for this annotation

GitHub Actions / build (hiddify-cli-linux-arm64, linux, arm64, linux-custom, ubuntu-20.04)

no required module provides package github.com/hiddify/hiddify-core/extension_repository/cleanip_scanner; to add it:

Check failure on line 13 in cmd/cmd_temp.go

View workflow job for this annotation

GitHub Actions / build (hiddify-cli-linux-mips-softfloat, linux, mips, softfloat, linux-custom, ubuntu-20.04)

no required module provides package github.com/hiddify/hiddify-core/extension_repository/cleanip_scanner; to add it:

Check failure on line 13 in cmd/cmd_temp.go

View workflow job for this annotation

GitHub Actions / build (hiddify-core-linux-amd64, ubuntu-20.04, linux-amd64)

no required module provides package github.com/hiddify/hiddify-core/extension_repository/cleanip_scanner; to add it:

Check failure on line 13 in cmd/cmd_temp.go

View workflow job for this annotation

GitHub Actions / build (hiddify-cli-linux-amd64, linux, amd64, v1, linux-custom, ubuntu-20.04)

no required module provides package github.com/hiddify/hiddify-core/extension_repository/cleanip_scanner; to add it:

Check failure on line 13 in cmd/cmd_temp.go

View workflow job for this annotation

GitHub Actions / build (hiddify-cli-linux-mips64el, linux, mips64le, linux-custom, ubuntu-20.04)

no required module provides package github.com/hiddify/hiddify-core/extension_repository/cleanip_scanner; to add it:

Check failure on line 13 in cmd/cmd_temp.go

View workflow job for this annotation

GitHub Actions / build (hiddify-cli-linux-mipsel-hardfloat, linux, mipsle, hardfloat, linux-custom, ubuntu-20.04)

no required module provides package github.com/hiddify/hiddify-core/extension_repository/cleanip_scanner; to add it:

Check failure on line 13 in cmd/cmd_temp.go

View workflow job for this annotation

GitHub Actions / build (hiddify-cli-linux-mipsel-softfloat, linux, mipsle, softfloat, linux-custom, ubuntu-20.04)

no required module provides package github.com/hiddify/hiddify-core/extension_repository/cleanip_scanner; to add it:

Check failure on line 13 in cmd/cmd_temp.go

View workflow job for this annotation

GitHub Actions / build (hiddify-cli-linux-s390x, linux, s390x, linux-custom, ubuntu-20.04)

no required module provides package github.com/hiddify/hiddify-core/extension_repository/cleanip_scanner; to add it:

Check failure on line 13 in cmd/cmd_temp.go

View workflow job for this annotation

GitHub Actions / build (hiddify-core-windows-amd64, ubuntu-latest, windows-amd64, x64)

no required module provides package github.com/hiddify/hiddify-core/extension_repository/cleanip_scanner; to add it:

Check failure on line 13 in cmd/cmd_temp.go

View workflow job for this annotation

GitHub Actions / build (hiddify-core-windows-amd64, ubuntu-latest, windows-amd64, x64)

no required module provides package github.com/hiddify/hiddify-core/extension_repository/cleanip_scanner; to add it:

Check failure on line 13 in cmd/cmd_temp.go

View workflow job for this annotation

GitHub Actions / build (hiddify-core-macos-universal, macos-12, macos-universal)

no required module provides package github.com/hiddify/hiddify-core/extension_repository/cleanip_scanner; to add it:

Check failure on line 13 in cmd/cmd_temp.go

View workflow job for this annotation

GitHub Actions / build (hiddify-core-macos-universal, macos-12, macos-universal)

no required module provides package github.com/hiddify/hiddify-core/extension_repository/cleanip_scanner; to add it:
"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())
}
Loading

0 comments on commit 6d58012

Please sign in to comment.