github.com/reapchain/go-reapchain@v0.2.15-0.20210609012950-9735c110c705/cmd/puppeth/module_faucet.go (about) 1 // Copyright 2017 The go-ethereum Authors 2 // This file is part of go-ethereum. 3 // 4 // go-ethereum is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // go-ethereum is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. 16 17 package main 18 19 import ( 20 "bytes" 21 "fmt" 22 "html/template" 23 "math/rand" 24 "path/filepath" 25 "strconv" 26 "strings" 27 28 "github.com/ethereum/go-ethereum/log" 29 ) 30 31 // faucetDockerfile is the Dockerfile required to build an faucet container to 32 // grant crypto tokens based on GitHub authentications. 33 var faucetDockerfile = ` 34 FROM alpine:latest 35 36 RUN mkdir /go 37 ENV GOPATH /go 38 39 RUN \ 40 apk add --update git go make gcc musl-dev ca-certificates linux-headers && \ 41 mkdir -p $GOPATH/src/github.com/ethereum && \ 42 (cd $GOPATH/src/github.com/ethereum && git clone --depth=1 https://github.com/ethereum/go-ethereum) && \ 43 go build -v github.com/ethereum/go-ethereum/cmd/faucet && \ 44 apk del git go make gcc musl-dev linux-headers && \ 45 rm -rf $GOPATH && rm -rf /var/cache/apk/* 46 47 ADD genesis.json /genesis.json 48 ADD account.json /account.json 49 ADD account.pass /account.pass 50 51 EXPOSE 8080 52 53 CMD [ \ 54 "/faucet", "--genesis", "/genesis.json", "--network", "{{.NetworkID}}", "--bootnodes", "{{.Bootnodes}}", "--ethstats", "{{.Ethstats}}", "--ethport", "{{.EthPort}}", \ 55 "--faucet.name", "{{.FaucetName}}", "--faucet.amount", "{{.FaucetAmount}}", "--faucet.minutes", "{{.FaucetMinutes}}", "--faucet.tiers", "{{.FaucetTiers}}", \ 56 "--github.user", "{{.GitHubUser}}", "--github.token", "{{.GitHubToken}}", "--account.json", "/account.json", "--account.pass", "/account.pass" \ 57 {{if .CaptchaToken}}, "--captcha.token", "{{.CaptchaToken}}", "--captcha.secret", "{{.CaptchaSecret}}"{{end}} \ 58 ]` 59 60 // faucetComposefile is the docker-compose.yml file required to deploy and maintain 61 // a crypto faucet. 62 var faucetComposefile = ` 63 version: '2' 64 services: 65 faucet: 66 build: . 67 image: {{.Network}}/faucet 68 ports: 69 - "{{.EthPort}}:{{.EthPort}}"{{if not .VHost}} 70 - "{{.ApiPort}}:8080"{{end}} 71 volumes: 72 - {{.Datadir}}:/root/.faucet 73 environment: 74 - ETH_PORT={{.EthPort}} 75 - ETH_NAME={{.EthName}} 76 - FAUCET_AMOUNT={{.FaucetAmount}} 77 - FAUCET_MINUTES={{.FaucetMinutes}} 78 - FAUCET_TIERS={{.FaucetTiers}} 79 - GITHUB_USER={{.GitHubUser}} 80 - GITHUB_TOKEN={{.GitHubToken}} 81 - CAPTCHA_TOKEN={{.CaptchaToken}} 82 - CAPTCHA_SECRET={{.CaptchaSecret}}{{if .VHost}} 83 - VIRTUAL_HOST={{.VHost}} 84 - VIRTUAL_PORT=8080{{end}} 85 restart: always 86 ` 87 88 // deployFaucet deploys a new faucet container to a remote machine via SSH, 89 // docker and docker-compose. If an instance with the specified network name 90 // already exists there, it will be overwritten! 91 func deployFaucet(client *sshClient, network string, bootnodes []string, config *faucetInfos) ([]byte, error) { 92 // Generate the content to upload to the server 93 workdir := fmt.Sprintf("%d", rand.Int63()) 94 files := make(map[string][]byte) 95 96 dockerfile := new(bytes.Buffer) 97 template.Must(template.New("").Parse(faucetDockerfile)).Execute(dockerfile, map[string]interface{}{ 98 "NetworkID": config.node.network, 99 "Bootnodes": strings.Join(bootnodes, ","), 100 "Ethstats": config.node.ethstats, 101 "EthPort": config.node.portFull, 102 "GitHubUser": config.githubUser, 103 "GitHubToken": config.githubToken, 104 "CaptchaToken": config.captchaToken, 105 "CaptchaSecret": config.captchaSecret, 106 "FaucetName": strings.Title(network), 107 "FaucetAmount": config.amount, 108 "FaucetMinutes": config.minutes, 109 "FaucetTiers": config.tiers, 110 }) 111 files[filepath.Join(workdir, "Dockerfile")] = dockerfile.Bytes() 112 113 composefile := new(bytes.Buffer) 114 template.Must(template.New("").Parse(faucetComposefile)).Execute(composefile, map[string]interface{}{ 115 "Network": network, 116 "Datadir": config.node.datadir, 117 "VHost": config.host, 118 "ApiPort": config.port, 119 "EthPort": config.node.portFull, 120 "EthName": config.node.ethstats[:strings.Index(config.node.ethstats, ":")], 121 "GitHubUser": config.githubUser, 122 "GitHubToken": config.githubToken, 123 "CaptchaToken": config.captchaToken, 124 "CaptchaSecret": config.captchaSecret, 125 "FaucetAmount": config.amount, 126 "FaucetMinutes": config.minutes, 127 "FaucetTiers": config.tiers, 128 }) 129 files[filepath.Join(workdir, "docker-compose.yaml")] = composefile.Bytes() 130 131 files[filepath.Join(workdir, "genesis.json")] = []byte(config.node.genesis) 132 files[filepath.Join(workdir, "account.json")] = []byte(config.node.keyJSON) 133 files[filepath.Join(workdir, "account.pass")] = []byte(config.node.keyPass) 134 135 // Upload the deployment files to the remote server (and clean up afterwards) 136 if out, err := client.Upload(files); err != nil { 137 return out, err 138 } 139 defer client.Run("rm -rf " + workdir) 140 141 // Build and deploy the faucet service 142 return nil, client.Stream(fmt.Sprintf("cd %s && docker-compose -p %s up -d --build", workdir, network)) 143 } 144 145 // faucetInfos is returned from an faucet status check to allow reporting various 146 // configuration parameters. 147 type faucetInfos struct { 148 node *nodeInfos 149 host string 150 port int 151 amount int 152 minutes int 153 tiers int 154 githubUser string 155 githubToken string 156 captchaToken string 157 captchaSecret string 158 } 159 160 // String implements the stringer interface. 161 func (info *faucetInfos) String() string { 162 return fmt.Sprintf("host=%s, api=%d, eth=%d, amount=%d, minutes=%d, tiers=%d, github=%s, captcha=%v, ethstats=%s", info.host, info.port, info.node.portFull, info.amount, info.minutes, info.tiers, info.githubUser, info.captchaToken != "", info.node.ethstats) 163 } 164 165 // checkFaucet does a health-check against an faucet server to verify whether 166 // it's running, and if yes, gathering a collection of useful infos about it. 167 func checkFaucet(client *sshClient, network string) (*faucetInfos, error) { 168 // Inspect a possible faucet container on the host 169 infos, err := inspectContainer(client, fmt.Sprintf("%s_faucet_1", network)) 170 if err != nil { 171 return nil, err 172 } 173 if !infos.running { 174 return nil, ErrServiceOffline 175 } 176 // Resolve the port from the host, or the reverse proxy 177 port := infos.portmap["8080/tcp"] 178 if port == 0 { 179 if proxy, _ := checkNginx(client, network); proxy != nil { 180 port = proxy.port 181 } 182 } 183 if port == 0 { 184 return nil, ErrNotExposed 185 } 186 // Resolve the host from the reverse-proxy and the config values 187 host := infos.envvars["VIRTUAL_HOST"] 188 if host == "" { 189 host = client.server 190 } 191 amount, _ := strconv.Atoi(infos.envvars["FAUCET_AMOUNT"]) 192 minutes, _ := strconv.Atoi(infos.envvars["FAUCET_MINUTES"]) 193 tiers, _ := strconv.Atoi(infos.envvars["FAUCET_TIERS"]) 194 195 // Retrieve the funding account informations 196 var out []byte 197 keyJSON, keyPass := "", "" 198 if out, err = client.Run(fmt.Sprintf("docker exec %s_faucet_1 cat /account.json", network)); err == nil { 199 keyJSON = string(bytes.TrimSpace(out)) 200 } 201 if out, err = client.Run(fmt.Sprintf("docker exec %s_faucet_1 cat /account.pass", network)); err == nil { 202 keyPass = string(bytes.TrimSpace(out)) 203 } 204 // Run a sanity check to see if the port is reachable 205 if err = checkPort(host, port); err != nil { 206 log.Warn("Faucet service seems unreachable", "server", host, "port", port, "err", err) 207 } 208 // Container available, assemble and return the useful infos 209 return &faucetInfos{ 210 node: &nodeInfos{ 211 datadir: infos.volumes["/root/.faucet"], 212 portFull: infos.portmap[infos.envvars["ETH_PORT"]+"/tcp"], 213 ethstats: infos.envvars["ETH_NAME"], 214 keyJSON: keyJSON, 215 keyPass: keyPass, 216 }, 217 host: host, 218 port: port, 219 amount: amount, 220 minutes: minutes, 221 tiers: tiers, 222 githubUser: infos.envvars["GITHUB_USER"], 223 githubToken: infos.envvars["GITHUB_TOKEN"], 224 captchaToken: infos.envvars["CAPTCHA_TOKEN"], 225 captchaSecret: infos.envvars["CAPTCHA_SECRET"], 226 }, nil 227 }