forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.go
214 lines (180 loc) · 4.56 KB
/
serve.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
204
205
206
207
208
209
210
211
212
213
214
package main
import (
"context"
"flag"
"fmt"
"regexp"
"strconv"
"time"
"github.com/gnolang/faucet"
tm2Client "github.com/gnolang/faucet/client/http"
"github.com/gnolang/faucet/config"
"github.com/gnolang/faucet/estimate/static"
"github.com/gnolang/gno/gno.land/pkg/log"
"github.com/gnolang/gno/tm2/pkg/commands"
"github.com/gnolang/gno/tm2/pkg/errors"
"github.com/gnolang/gno/tm2/pkg/std"
"go.uber.org/zap/zapcore"
)
const (
defaultGasFee = "1000000ugnot"
defaultGasWanted = "100000"
defaultRemote = "http://127.0.0.1:26657"
defaultListenAddress = "127.0.0.1:5050"
)
// url & struct for verify captcha
const siteVerifyURL = "https://www.google.com/recaptcha/api/siteverify"
const (
ipv6Loopback = "::1"
ipv6ZeroAddr = "0:0:0:0:0:0:0:1"
ipv4Loopback = "127.0.0.1"
)
var remoteRegex = regexp.MustCompile(`^https?://[a-z\d.-]+(:\d+)?(?:/[a-z\d]+)*$`)
var errInvalidCaptcha = errors.New("unable to verify captcha")
type SiteVerifyResponse struct {
Success bool `json:"success"`
Score float64 `json:"score"`
Action string `json:"action"`
ChallengeTS time.Time `json:"challenge_ts"`
Hostname string `json:"hostname"`
ErrorCodes []string `json:"error-codes"`
}
type serveCfg struct {
listenAddress string
chainID string
mnemonic string
maxSendAmount string
numAccounts uint64
remote string
captchaSecret string
isBehindProxy bool
}
func newServeCmd() *commands.Command {
cfg := &serveCfg{}
return commands.NewCommand(
commands.Metadata{
Name: "serve",
ShortUsage: "serve [flags]",
LongHelp: "Serves the gno.land faucet to users",
},
cfg,
func(ctx context.Context, args []string) error {
return execServe(ctx, cfg, commands.NewDefaultIO())
},
)
}
func (c *serveCfg) RegisterFlags(fs *flag.FlagSet) {
fs.StringVar(
&c.listenAddress,
"listen-address",
defaultListenAddress,
"the faucet server listen address",
)
fs.StringVar(
&c.remote,
"remote",
defaultRemote,
"remote node URL",
)
fs.StringVar(
&c.mnemonic,
"mnemonic",
"",
"the mnemonic for faucet keys",
)
fs.Uint64Var(
&c.numAccounts,
"num-accounts",
1,
"the number of faucet accounts, based on the mnemonic",
)
fs.StringVar(
&c.chainID,
"chain-id",
"",
"the chain ID associated with the remote Gno chain",
)
fs.StringVar(
&c.maxSendAmount,
"max-send-amount",
"10000000ugnot",
"the static max send amount (native currency)",
)
fs.StringVar(
&c.captchaSecret,
"captcha-secret",
"",
"recaptcha secret key (if empty, captcha are disabled)",
)
fs.BoolVar(
&c.isBehindProxy,
"is-behind-proxy",
false,
"use X-Forwarded-For IP for throttling",
)
}
// generateFaucetConfig generates the Faucet configuration
// based on the flag data
func (c *serveCfg) generateFaucetConfig() *config.Config {
// Create the default configuration
cfg := config.DefaultConfig()
cfg.ListenAddress = c.listenAddress
cfg.ChainID = c.chainID
cfg.Mnemonic = c.mnemonic
cfg.MaxSendAmount = c.maxSendAmount
cfg.NumAccounts = c.numAccounts
return cfg
}
func execServe(ctx context.Context, cfg *serveCfg, io commands.IO) error {
// Parse static gas values.
// It is worth noting that this is temporary,
// and will be removed once gas estimation is enabled
// on gno.land
gasFee := std.MustParseCoin(defaultGasFee)
gasWanted, err := strconv.ParseInt(defaultGasWanted, 10, 64)
if err != nil {
return fmt.Errorf("invalid gas wanted, %w", err)
}
// Parse the send amount
_, err = std.ParseCoins(cfg.maxSendAmount)
if err != nil {
return fmt.Errorf("invalid send amount, %w", err)
}
// Validate the remote address
if !remoteRegex.MatchString(cfg.remote) {
return errors.New("invalid remote address")
}
// Create the client (HTTP)
cli, err := tm2Client.NewClient(cfg.remote)
if err != nil {
return fmt.Errorf("unable to create TM2 client, %w", err)
}
// Set up the logger
logger := log.ZapLoggerToSlog(
log.NewZapJSONLogger(
io.Out(),
zapcore.DebugLevel,
),
)
// Start throttled faucet.
st := newIPThrottler(defaultRateLimitInterval, defaultCleanTimeout)
st.start(ctx)
// Prepare the middlewares
middlewares := []faucet.Middleware{
getIPMiddleware(cfg.isBehindProxy, st),
getCaptchaMiddleware(cfg.captchaSecret),
}
// Create a new faucet with
// static gas estimation
f, err := faucet.NewFaucet(
static.New(gasFee, gasWanted),
cli,
faucet.WithLogger(logger),
faucet.WithConfig(cfg.generateFaucetConfig()),
faucet.WithMiddlewares(middlewares),
)
if err != nil {
return fmt.Errorf("unable to create faucet, %w", err)
}
return f.Serve(ctx)
}