This repository has been archived by the owner on Mar 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
203 lines (178 loc) · 5.27 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
package main
import (
"crypto/sha256"
"flag"
"fmt"
"github.com/dimiro1/banner"
"github.com/gorilla/mux"
"github.com/joho/godotenv"
neo "github.com/nspcc-dev/neo-go/pkg/rpcclient"
log "github.com/sirupsen/logrus"
"net/http"
"os"
"strconv"
"strings"
"time"
)
type Signature struct {
Raw []byte `json:"raw"`
Hash [32]byte `json:"hash"`
R [32]byte `json:"r"`
S [32]byte `json:"s"`
V uint8 `json:"v"`
}
type Timewarp struct {
Offset int `json:"offset"`
}
type Blockchain struct {
Contract string
PrivateKey string
Url string
Deploy bool
}
type Opts struct {
Port int
Env string
HS256 string
Ethereum Blockchain
NEO Blockchain
Admins string
}
var (
opts *Opts
jwtKey []byte
ethClient *ClientETH
neoClient *neo.Client
debug bool
secondsAdd int
admins []string
)
func NewOpts() *Opts {
err := godotenv.Load()
if err != nil {
log.Printf("Could not find env file [%v], using defaults", err)
}
o := &Opts{}
flag.StringVar(&o.Env, "env", lookupEnv("ENV"), "ENV variable")
flag.IntVar(&o.Port, "port", lookupEnvInt("PORT",
9084), "listening HTTP port")
flag.StringVar(&o.HS256, "hs256", lookupEnv("HS256"), "HS256 key")
flag.StringVar(&o.Ethereum.PrivateKey, "eth-private-key", lookupEnv("ETH_PRIVATE_KEY"), "Ethereum private key")
flag.StringVar(&o.Ethereum.Contract, "eth-contract", lookupEnv("ETH_CONTRACT"), "Ethereum contract address")
flag.StringVar(&o.Ethereum.Url, "eth-url", lookupEnv("ETH_URL"), "Ethereum URL")
flag.BoolVar(&o.Ethereum.Deploy, "eth-deploy", lookupEnv("ETH_DEPLOY") == "true", "Set to true to deploy ETH contract")
flag.StringVar(&o.NEO.PrivateKey, "neo-private-key", lookupEnv("NEO_PRIVATE_KEY"), "NEO private key")
flag.StringVar(&o.NEO.Contract, "neo-contract", lookupEnv("NEO_CONTRACT"), "NEO contract address")
flag.StringVar(&o.NEO.Url, "neo-url", lookupEnv("NEO_URL"), "NEO URL")
flag.BoolVar(&o.NEO.Deploy, "neo-deploy", lookupEnv("NEO_DEPLOY") == "true", "Set to true to deploy NEO contract")
flag.StringVar(&o.Admins, "admins", lookupEnv("ADMINS"), "Admins")
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
}
flag.Parse()
//set defaults, be explicit
if o.Env == "local" || o.Env == "dev" {
debug = true
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}
if o.HS256 != "" {
h := sha256.New()
h.Write([]byte(o.HS256))
jwtKey = h.Sum(nil)
log.Debugf("jwtKey: %v", jwtKey)
} else {
log.Fatalf("HS256 seed is required, non was provided")
}
admins = strings.Split(o.Admins, ";")
if strings.HasPrefix(o.Ethereum.PrivateKey, "0x") {
o.Ethereum.PrivateKey = o.Ethereum.PrivateKey[2:]
}
return o
}
func lookupEnv(key string, defaultValues ...string) string {
if val, ok := os.LookupEnv(key); ok {
return val
}
for _, v := range defaultValues {
if v != "" {
return v
}
}
return ""
}
func lookupEnvInt(key string, defaultValues ...int) int {
if val, ok := os.LookupEnv(key); ok {
v, err := strconv.Atoi(val)
if err != nil {
log.Printf("LookupEnvInt[%s]: %v", key, err)
return 0
}
return v
}
for _, v := range defaultValues {
if v != 0 {
return v
}
}
return 0
}
func ethInit() *ClientETH {
now := time.Now()
ethClient, err := getEthClient(opts.Ethereum.Url, opts.Ethereum.PrivateKey, opts.Ethereum.Deploy, opts.Ethereum.Contract)
for err != nil && now.Add(time.Duration(10)*time.Second).After(time.Now()) {
time.Sleep(time.Second)
ethClient, err = getEthClient(opts.Ethereum.Url, opts.Ethereum.PrivateKey, opts.Ethereum.Deploy, opts.Ethereum.Contract)
}
if err != nil {
log.Fatal("Could not initialize ETH network", err)
}
return ethClient
}
func neoInit() *neo.Client {
now := time.Now()
neoClient, err := getNeoClient(opts.NEO.Url)
for err != nil && now.Add(time.Duration(10)*time.Second).After(time.Now()) {
time.Sleep(time.Second)
neoClient, err = getNeoClient(opts.NEO.Url)
}
if err != nil {
//log.Fatal("Could not initialize NEO network", err)
log.Debugf("Could not initialize NEO network", err)
}
return neoClient
}
func timeNow() time.Time {
if debug {
return time.Now().Add(time.Duration(secondsAdd) * time.Second).UTC()
} else {
return time.Now().UTC()
}
}
func main() {
f, err := os.Open("banner.txt")
if err == nil {
banner.Init(os.Stdout, true, false, f)
} else {
log.Printf("could not display banner...")
}
opts = NewOpts()
ethClient = ethInit()
neoClient = neoInit()
// only internal routes, not accessible through caddy server
router := mux.NewRouter()
//this can only be called by an internal server
router.HandleFunc("/admin/sign/{userId}/{totalPayedOut}", jwtAuth(jwtAuthServer(sign))).Methods(http.MethodPost)
//this can be called from frontend, but only the admin
if debug {
router.HandleFunc("/admin/time", jwtAuth(jwtAuthAdmin(serverTime, admins))).Methods(http.MethodGet)
router.HandleFunc("/admin/time/eth", jwtAuth(jwtAuthAdmin(serverTimeEth, admins))).Methods(http.MethodGet)
router.HandleFunc("/admin/timewarp/{hours}", jwtAuth(jwtAuthAdmin(timeWarp, admins))).Methods(http.MethodPost)
}
//available for the public
router.HandleFunc("/config", config).Methods(http.MethodGet)
log.Printf("listing on port %v", opts.Port)
log.Fatal(http.ListenAndServe(":"+strconv.Itoa(opts.Port), router))
}