-
Notifications
You must be signed in to change notification settings - Fork 0
/
clienthost.go
190 lines (179 loc) · 5.26 KB
/
clienthost.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package ubot
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/1354092549/wsrpc"
"github.com/gorilla/websocket"
)
type RPCConfigurator func(rpc *wsrpc.WebsocketRPC, rpcConn *wsrpc.WebsocketRPCConn) error
type ClientRegistrar func(managerUrl *url.URL, manager *Manager) (string, error)
func AssertNoError(err error) {
if err != nil {
panic(err)
}
}
func DialRouter(operator string, urlStr string, registerClient ClientRegistrar) (*websocket.Conn, error) {
switch strings.ToLower(operator) {
case "applyto":
managerUrl, err := url.Parse(urlStr)
if err != nil {
return nil, err
}
if managerUrl.User != nil {
user := managerUrl.User.Username()
password, _ := managerUrl.User.Password()
userData := make(url.Values)
userData["user"] = []string{user}
userData["password"] = []string{password}
getTokenUrl := *managerUrl
getTokenUrl.User = nil
if getTokenUrl.Scheme == "wss" {
getTokenUrl.Scheme = "https"
} else {
getTokenUrl.Scheme = "http"
}
getTokenUrl.Path = "/api/manager/get_token"
getTokenUrlStr := getTokenUrl.String()
fmt.Printf("Getting manager token from %s\n", getTokenUrlStr)
res, err := http.PostForm(getTokenUrlStr, userData)
if err != nil {
return nil, errors.New("failed to get the manager token: " + err.Error())
}
if res.StatusCode != http.StatusOK {
return nil, errors.New("failed to get the manager token: " + res.Status)
}
defer res.Body.Close()
managerTokenBytes, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, errors.New("failed to get the manager token: " + err.Error())
}
managerToken := string(managerTokenBytes)
fmt.Println("Got manager token")
managerUrl.User = nil
managerUrl.RawQuery = "token=" + managerToken
}
managerUrlStr := managerUrl.String()
fmt.Printf("Applying to %s\n", managerUrlStr)
managerConn, _, err := websocket.DefaultDialer.Dial(managerUrlStr, nil)
if err != nil {
return nil, errors.New("failed to dial to manager: " + err.Error())
}
managerRPCConn := wsrpc.NewWebsocketRPC().Connect(managerConn)
go func() {
managerRPCConn.ServeConn()
managerConn.Close()
}()
var manager Manager
manager.Get(managerRPCConn)
urlStr, err = registerClient(managerUrl, &manager)
_ = managerConn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
if err != nil {
return nil, errors.New("failed to register the client: " + err.Error())
}
fmt.Println("Applied")
case "connect":
break
default:
return nil, errors.New("unknown operator: " + operator)
}
fmt.Printf("Connecting to %s\n", urlStr)
conn, _, err := websocket.DefaultDialer.Dial(urlStr, nil)
if err != nil {
return nil, errors.New("failed to dial to router: " + err.Error())
}
return conn, nil
}
func HostClient(registerClient ClientRegistrar, configRPC RPCConfigurator) error {
var err error
fmt.Println("Press Ctrl+C to shut down.")
if len(os.Args) < 3 {
return errors.New("missing required arguments")
}
operator, urlStr := os.Args[1], os.Args[2]
var conn *websocket.Conn
for retryCount := 0; retryCount < 5; retryCount++ {
conn, err = DialRouter(operator, urlStr, registerClient)
if err == nil {
break
}
fmt.Println("Failed to connect to UBot Router, it will try again in 5 seconds.")
time.Sleep(5 * time.Second)
}
if err != nil {
return fmt.Errorf("Failed to connect to UBot Router after 5 attempts: %v", err)
}
fmt.Println("Connected")
rpc := wsrpc.NewWebsocketRPC()
rpcConn := rpc.Connect(conn)
err = configRPC(rpc, rpcConn)
if err != nil {
return err
}
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
fn := make(chan int, 1)
go func() {
rpcConn.ServeConn()
conn.Close()
fn <- 0
}()
select {
case <-sc:
fmt.Println("Shutting down...")
_ = conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
<-fn
case <-fn:
fmt.Println("Session ended")
}
return nil
}
func HostAccount(id string, creater func(*AccountEventEmitter) *Account) error {
return HostClient(func(managerUrl *url.URL, manager *Manager) (string, error) {
token, err := manager.RegisterAccount(id)
if err != nil {
return "", err
}
urlObj := *managerUrl
urlObj.Path = "/api/account"
query := url.Values{}
query.Set("id", id)
query.Set("token", token)
urlObj.RawQuery = query.Encode()
return urlObj.String(), nil
}, func(rpc *wsrpc.WebsocketRPC, rpcConn *wsrpc.WebsocketRPCConn) error {
remoteObj := new(AccountEventEmitter)
remoteObj.Get(rpcConn)
localObj := creater(remoteObj)
localObj.Register(rpc)
return nil
})
}
func HostApp(id string, creater func(*AppApi) *App) error {
return HostClient(func(managerUrl *url.URL, manager *Manager) (string, error) {
token, err := manager.RegisterApp(id)
if err != nil {
return "", err
}
urlObj := *managerUrl
urlObj.Path = "/api/app"
query := url.Values{}
query.Set("id", id)
query.Set("token", token)
urlObj.RawQuery = query.Encode()
return urlObj.String(), nil
}, func(rpc *wsrpc.WebsocketRPC, rpcConn *wsrpc.WebsocketRPCConn) error {
remoteObj := new(AppApi)
remoteObj.Get(rpcConn)
localObj := creater(remoteObj)
localObj.Register(rpc)
return nil
})
}