-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlocal.go
151 lines (125 loc) · 3.8 KB
/
local.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
146
147
148
149
150
151
package slss
import (
"bytes"
"encoding/json"
"fmt"
"net"
"net/http"
"os/exec"
"time"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
const (
localCliServerAddrTemplate = "-s %v"
localCliPasswordTemplate = "-k %v"
localCliServerPortTemplate = "-p %v"
localCliLocalPortTemplate = "-l %v"
)
// Init starts the slss
func Init(config *Config, funcConfig *FuncConfig) {
interval, err := time.ParseDuration(fmt.Sprintf("%vs", funcConfig.Timeout-15))
if err != nil {
PrintErrorAndExit(err)
}
apexExecutor := &APEXCommandExecutor{Config: config}
log.Info("[slss] Uploading lambda function...")
if err := UploadFunc(apexExecutor); err != nil {
PrintErrorAndExit(err)
}
remoteProxyAddrChan := GetProxyAddrChan(config)
log.Info("[slss] Creating ngrox proxy...")
proxyAddr, err := StartNgrokProxy(config.Ngrok, ProxyProtoHTTP, config.LocalServerPort)
if err != nil {
PrintErrorAndExit(err)
}
log.Info("[slss] Ngrox address: ", proxyAddr)
go func() {
log.Info("[slss] Requesting lambda function...")
go func() {
if err := RequestRemoteFunc(apexExecutor, proxyAddr); err != nil {
log.Errorln(err)
}
}()
for range time.Tick(interval) {
log.Info("[slss] Requesting lambda function...")
go func() {
if err := RequestRemoteFunc(apexExecutor, proxyAddr); err != nil {
log.Errorln(err)
}
}()
}
}()
var localCliCmd *exec.Cmd
for remoteProxyAddr := range remoteProxyAddrChan {
log.Info("[slss] Remote proxy address: ", remoteProxyAddr)
log.Info("[slss] Restarting local ss client...")
if localCliCmd != nil {
if err := localCliCmd.Process.Kill(); err != nil {
PrintErrorAndExit(err)
}
}
localCliCmd, err = StartLocalClient(config, remoteProxyAddr)
if err != nil {
PrintErrorAndExit(err)
}
log.Info("[slss] Local ss client started at 127.0.0.1:", config.Shadowsocks.LocalPort)
}
}
// StartLocalClient starts a slss client
func StartLocalClient(config *Config, proxyAddr string) (*exec.Cmd, error) {
host, port, err := net.SplitHostPort(proxyAddr)
if err != nil {
return nil, errors.WithStack(err)
}
cmd := exec.Command(
"./bin/shadowsocks_local",
fmt.Sprintf(localCliServerAddrTemplate, host),
fmt.Sprintf(localCliServerPortTemplate, port),
fmt.Sprintf(localCliLocalPortTemplate, config.Shadowsocks.LocalPort),
fmt.Sprintf(localCliPasswordTemplate, config.Shadowsocks.Password),
)
if err := cmd.Start(); err != nil {
return nil, errors.WithStack(err)
}
return cmd, nil
}
// UploadFunc uploads the slss function to AWS lambda
func UploadFunc(executor *APEXCommandExecutor) error {
_, err := executor.Exec("apex", nil, "deploy", "slss")
return errors.WithStack(err)
}
// RequestRemoteFunc sends a request to the slss function in AWS lambda
func RequestRemoteFunc(executor *APEXCommandExecutor, proxyAddr string) error {
lambdaMessage, err := json.Marshal(LambdaShadowSocksConfig{
Port: "10808",
Method: executor.Config.Shadowsocks.Method,
Password: executor.Config.Shadowsocks.Password,
ProxyURL: proxyAddr,
NgrokToken: executor.Config.Ngrok.AuthToken,
})
if err != nil {
return errors.WithStack(err)
}
_, err = executor.Exec("apex", bytes.NewBufferString(string(lambdaMessage)), "invoke", "slss")
return errors.WithStack(err)
}
// GetProxyAddrChan starts a local server for remote ss server proxy address
func GetProxyAddrChan(config *Config) chan string {
ch := make(chan string)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
ssServerAddr := r.URL.Query().Get("ss_server_addr")
if ssServerAddr == "" {
w.WriteHeader(http.StatusBadRequest)
return
}
ch <- ssServerAddr
})
go func() {
if err := http.ListenAndServe(":"+config.LocalServerPort, nil); err != nil {
log.Errorln(err)
}
}()
log.Info("[slss] Local addr chan listen at " + config.LocalServerPort)
return ch
}