-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
146 lines (123 loc) · 3.72 KB
/
main.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
package main
import (
"fmt"
"log"
"os"
"os/exec"
"sync"
"syscall"
"github.com/script-development/rtcv_scraper_client/v2/crypto"
)
func main() {
env := mustReadEnv()
api := NewAPI()
credentials := []SetCredentialsArg{env.PrimaryServer.toCredArg(true)}
for _, server := range env.AlternativeServers {
credentials = append(credentials, server.toCredArg(false))
}
var err error
var loginUsers []EnvUser
if !env.MockMode {
err = api.SetCredentials(credentials)
if err != nil {
log.Fatal(err)
}
decryptionKey := crypto.LoadAndVerivyKeys(env.PublicKey, env.PrivateKey)
fmt.Println("credentials set")
fmt.Println("testing connections..")
loginUsers = testServerConnections(api, credentials[0].APIKeyID, decryptionKey)
fmt.Println("connected to RTCV")
} else {
api.SetMockMode()
loginUsers = env.MockUsers
fmt.Println("In mock mode")
fmt.Println("You can turn this off in `env.json` by setting `mock_mode` to false")
}
api.ConnectToAllWebsockets()
useAddress := startWebserver(env, api, loginUsers)
healthCheckPort := os.Getenv("RTCV_SCRAPER_CLIENT_HEALTH_CHECK_PORT")
if healthCheckPort != "" {
go startHealthCheckServer(healthCheckPort)
}
fmt.Println("running scraper..")
if len(os.Args) <= 1 {
log.Fatal("must provide a command to run, for example: rtcv_scraper_client npm run scraper")
}
scraper := exec.Command(os.Args[1], os.Args[2:]...)
scraper.Env = append(os.Environ(), "SCRAPER_ADDRESS="+useAddress)
// Piple output of scraper to stdout
scraper.Stdin = os.Stdin
scraper.Stdout = os.Stdout
scraper.Stderr = os.Stderr
err = scraper.Run()
if err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
os.Exit(status.ExitStatus())
}
}
log.Fatal(err)
}
}
func testServerConnections(api *API, apiKeyID string, decryptionKey *crypto.Key) []EnvUser {
var wg sync.WaitGroup
for _, conn := range api.connections {
wg.Add(1)
go func(conn serverConn) {
err := conn.Get("/api/v1/health", nil)
if err != nil {
log.Fatal(err)
}
apiKeyInfo := struct {
Roles []struct {
Role uint64 `json:"role"`
} `json:"roles"`
}{}
err = conn.Get("/api/v1/auth/keyinfo", &apiKeyInfo)
if err != nil {
log.Fatal(err)
}
hasScraperRole := false
for _, role := range apiKeyInfo.Roles {
if role.Role == 1 {
hasScraperRole = true
break
}
}
if !hasScraperRole {
log.Fatal("provided key does not have scraper role (nr 1)")
}
wg.Done()
}(conn)
}
scraperUsers := struct {
ScraperPublicKey string `json:"scraperPubKey"`
Users []EnvUser `json:"users"`
}{}
err := api.connections[0].Get("/api/v1/scraperUsers/"+apiKeyID, &scraperUsers)
if err != nil {
// Wait for the connections above to complete checking before we do this error check but do the request already so we don't have to wait for that
// If one of the connections has an error they will throw
wg.Wait()
log.Fatal(err)
}
if scraperUsers.ScraperPublicKey != "" && scraperUsers.ScraperPublicKey != decryptionKey.PublicBase64 {
log.Fatal("the env.json provided contains a diffrent public key than registered in RTCV, scraper users won't be able to be decrypted")
}
loginUsers := []EnvUser{}
for _, user := range scraperUsers.Users {
if user.EncryptedPassword != "" {
user.Password, err = decryptionKey.DecryptScraperPassword(user.EncryptedPassword)
if err != nil {
log.Fatal("unable to decrypt password for user " + user.Username + ", error: " + err.Error())
}
loginUsers = append(loginUsers, user)
} else if user.Password != "" {
loginUsers = append(loginUsers, user)
} else {
fmt.Println("WARN: unusable login user", user.Username)
}
}
wg.Wait()
return loginUsers
}