-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlibv2ray_chisel.go
145 lines (126 loc) · 3.15 KB
/
libv2ray_chisel.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package libv2ray
import "C"
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"strings"
"time"
chclient "github.com/DevTomy/lib2chisel/client"
"github.com/DevTomy/lib2chisel/share/cos"
)
type multiFlag struct {
values *[]string
}
func (flag multiFlag) String() string {
return strings.Join(*flag.values, ", ")
}
func (flag multiFlag) Set(arg string) error {
*flag.values = append(*flag.values, arg)
return nil
}
type headerFlags struct {
http.Header
}
var globalClient *chclient.Client
//export ChiselStart
func ChiselStart(commands string) bool {
chclient.ClientStatus = "null"
var args = strings.Split(commands, " ")
if len(args) > 0 {
args = args[1:]
}
go client(args)
// Handle Client Connection Status For Android
timeout := time.After(10 * time.Second)
for {
select {
case <-timeout:
fmt.Println("timeout: ", timeout)
globalClient.Close()
return false
default:
if chclient.ClientStatus == "connected" {
return true
}
}
}
}
//export ChiselClose
func ChiselClose() bool {
globalClient.Close()
return true
}
func (flag *headerFlags) String() string {
out := ""
for k, v := range flag.Header {
out += fmt.Sprintf("%s: %s\n", k, v)
}
return out
}
func (flag *headerFlags) Set(arg string) error {
index := strings.Index(arg, ":")
if index < 0 {
return fmt.Errorf(`Invalid header (%s). Should be in the format "HeaderName: HeaderContent"`, arg)
}
if flag.Header == nil {
flag.Header = http.Header{}
}
key := arg[0:index]
value := arg[index+1:]
flag.Header.Set(key, strings.TrimSpace(value))
return nil
}
func client(args []string) error {
flags := flag.NewFlagSet("client", flag.ContinueOnError)
config := chclient.Config{Headers: http.Header{}}
flags.StringVar(&config.Fingerprint, "fingerprint", "", "")
flags.StringVar(&config.Auth, "auth", "", "")
flags.DurationVar(&config.KeepAlive, "keepalive", 25*time.Second, "")
flags.IntVar(&config.MaxRetryCount, "max-retry-count", -1, "")
flags.DurationVar(&config.MaxRetryInterval, "max-retry-interval", 0, "")
flags.StringVar(&config.Proxy, "proxy", "", "")
flags.StringVar(&config.TLS.CA, "tls-ca", "", "")
flags.BoolVar(&config.TLS.SkipVerify, "tls-skip-verify", false, "")
flags.StringVar(&config.TLS.Cert, "tls-cert", "", "")
flags.StringVar(&config.TLS.Key, "tls-key", "", "")
flags.Var(&headerFlags{config.Headers}, "header", "")
hostname := flags.String("hostname", "", "")
sni := flags.String("sni", "", "")
verbose := flags.Bool("v", false, "")
flags.Parse(args)
//pull out options, put back remaining args
args = flags.Args()
if len(args) < 2 {
log.Fatalf("A server and least one remote is required")
}
config.Server = args[0]
config.Remotes = args[1:]
//default auth
if config.Auth == "" {
config.Auth = os.Getenv("AUTH")
}
//move hostname onto headers
if *hostname != "" {
config.Headers.Set("Host", *hostname)
config.TLS.ServerName = *hostname
}
if *sni != "" {
config.TLS.ServerName = *sni
}
//ready
c, err := chclient.NewClient(&config)
if err != nil {
}
globalClient = c
c.Debug = *verbose
go cos.GoStats()
ctx := cos.InterruptContext()
if err := c.Start(ctx); err != nil {
}
if err := c.Wait(); err != nil {
}
return err
}