module_faucet.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. package main
  17. import (
  18. "bytes"
  19. "encoding/json"
  20. "fmt"
  21. "html/template"
  22. "math/rand"
  23. "path/filepath"
  24. "strconv"
  25. "strings"
  26. "github.com/ethereum/go-ethereum/common"
  27. "github.com/ethereum/go-ethereum/log"
  28. )
  29. // faucetDockerfile is the Dockerfile required to build a faucet container to
  30. // grant crypto tokens based on GitHub authentications.
  31. var faucetDockerfile = `
  32. FROM ethereum/client-go:alltools-latest
  33. ADD genesis.json /genesis.json
  34. ADD account.json /account.json
  35. ADD account.pass /account.pass
  36. EXPOSE 8080 30303 30303/udp
  37. ENTRYPOINT [ \
  38. "faucet", "--genesis", "/genesis.json", "--network", "{{.NetworkID}}", "--bootnodes", "{{.Bootnodes}}", "--ethstats", "{{.Ethstats}}", "--ethport", "{{.EthPort}}", \
  39. "--faucet.name", "{{.FaucetName}}", "--faucet.amount", "{{.FaucetAmount}}", "--faucet.minutes", "{{.FaucetMinutes}}", "--faucet.tiers", "{{.FaucetTiers}}", \
  40. "--account.json", "/account.json", "--account.pass", "/account.pass" \
  41. {{if .CaptchaToken}}, "--captcha.token", "{{.CaptchaToken}}", "--captcha.secret", "{{.CaptchaSecret}}"{{end}}{{if .NoAuth}}, "--noauth"{{end}} \
  42. {{if .TwitterToken}}, "--twitter.token.v1", "{{.TwitterToken}}"{{end}} \
  43. ]`
  44. // faucetComposefile is the docker-compose.yml file required to deploy and maintain
  45. // a crypto faucet.
  46. var faucetComposefile = `
  47. version: '2'
  48. services:
  49. faucet:
  50. build: .
  51. image: {{.Network}}/faucet
  52. container_name: {{.Network}}_faucet_1
  53. ports:
  54. - "{{.EthPort}}:{{.EthPort}}"
  55. - "{{.EthPort}}:{{.EthPort}}/udp"{{if not .VHost}}
  56. - "{{.ApiPort}}:8080"{{end}}
  57. volumes:
  58. - {{.Datadir}}:/root/.faucet
  59. environment:
  60. - ETH_PORT={{.EthPort}}
  61. - ETH_NAME={{.EthName}}
  62. - FAUCET_AMOUNT={{.FaucetAmount}}
  63. - FAUCET_MINUTES={{.FaucetMinutes}}
  64. - FAUCET_TIERS={{.FaucetTiers}}
  65. - CAPTCHA_TOKEN={{.CaptchaToken}}
  66. - CAPTCHA_SECRET={{.CaptchaSecret}}
  67. - TWITTER_TOKEN={{.TwitterToken}}
  68. - NO_AUTH={{.NoAuth}}{{if .VHost}}
  69. - VIRTUAL_HOST={{.VHost}}
  70. - VIRTUAL_PORT=8080{{end}}
  71. logging:
  72. driver: "json-file"
  73. options:
  74. max-size: "1m"
  75. max-file: "10"
  76. restart: always
  77. `
  78. // deployFaucet deploys a new faucet container to a remote machine via SSH,
  79. // docker and docker-compose. If an instance with the specified network name
  80. // already exists there, it will be overwritten!
  81. func deployFaucet(client *sshClient, network string, bootnodes []string, config *faucetInfos, nocache bool) ([]byte, error) {
  82. // Generate the content to upload to the server
  83. workdir := fmt.Sprintf("%d", rand.Int63())
  84. files := make(map[string][]byte)
  85. dockerfile := new(bytes.Buffer)
  86. template.Must(template.New("").Parse(faucetDockerfile)).Execute(dockerfile, map[string]interface{}{
  87. "NetworkID": config.node.network,
  88. "Bootnodes": strings.Join(bootnodes, ","),
  89. "Ethstats": config.node.ethstats,
  90. "EthPort": config.node.port,
  91. "CaptchaToken": config.captchaToken,
  92. "CaptchaSecret": config.captchaSecret,
  93. "FaucetName": strings.Title(network),
  94. "FaucetAmount": config.amount,
  95. "FaucetMinutes": config.minutes,
  96. "FaucetTiers": config.tiers,
  97. "NoAuth": config.noauth,
  98. "TwitterToken": config.twitterToken,
  99. })
  100. files[filepath.Join(workdir, "Dockerfile")] = dockerfile.Bytes()
  101. composefile := new(bytes.Buffer)
  102. template.Must(template.New("").Parse(faucetComposefile)).Execute(composefile, map[string]interface{}{
  103. "Network": network,
  104. "Datadir": config.node.datadir,
  105. "VHost": config.host,
  106. "ApiPort": config.port,
  107. "EthPort": config.node.port,
  108. "EthName": config.node.ethstats[:strings.Index(config.node.ethstats, ":")],
  109. "CaptchaToken": config.captchaToken,
  110. "CaptchaSecret": config.captchaSecret,
  111. "FaucetAmount": config.amount,
  112. "FaucetMinutes": config.minutes,
  113. "FaucetTiers": config.tiers,
  114. "NoAuth": config.noauth,
  115. "TwitterToken": config.twitterToken,
  116. })
  117. files[filepath.Join(workdir, "docker-compose.yaml")] = composefile.Bytes()
  118. files[filepath.Join(workdir, "genesis.json")] = config.node.genesis
  119. files[filepath.Join(workdir, "account.json")] = []byte(config.node.keyJSON)
  120. files[filepath.Join(workdir, "account.pass")] = []byte(config.node.keyPass)
  121. // Upload the deployment files to the remote server (and clean up afterwards)
  122. if out, err := client.Upload(files); err != nil {
  123. return out, err
  124. }
  125. defer client.Run("rm -rf " + workdir)
  126. // Build and deploy the faucet service
  127. if nocache {
  128. return nil, client.Stream(fmt.Sprintf("cd %s && docker-compose -p %s build --pull --no-cache && docker-compose -p %s up -d --force-recreate --timeout 60", workdir, network, network))
  129. }
  130. return nil, client.Stream(fmt.Sprintf("cd %s && docker-compose -p %s up -d --build --force-recreate --timeout 60", workdir, network))
  131. }
  132. // faucetInfos is returned from a faucet status check to allow reporting various
  133. // configuration parameters.
  134. type faucetInfos struct {
  135. node *nodeInfos
  136. host string
  137. port int
  138. amount int
  139. minutes int
  140. tiers int
  141. noauth bool
  142. captchaToken string
  143. captchaSecret string
  144. twitterToken string
  145. }
  146. // Report converts the typed struct into a plain string->string map, containing
  147. // most - but not all - fields for reporting to the user.
  148. func (info *faucetInfos) Report() map[string]string {
  149. report := map[string]string{
  150. "Website address": info.host,
  151. "Website listener port": strconv.Itoa(info.port),
  152. "Ethereum listener port": strconv.Itoa(info.node.port),
  153. "Funding amount (base tier)": fmt.Sprintf("%d Ethers", info.amount),
  154. "Funding cooldown (base tier)": fmt.Sprintf("%d mins", info.minutes),
  155. "Funding tiers": strconv.Itoa(info.tiers),
  156. "Captha protection": fmt.Sprintf("%v", info.captchaToken != ""),
  157. "Using Twitter API": fmt.Sprintf("%v", info.twitterToken != ""),
  158. "Ethstats username": info.node.ethstats,
  159. }
  160. if info.noauth {
  161. report["Debug mode (no auth)"] = "enabled"
  162. }
  163. if info.node.keyJSON != "" {
  164. var key struct {
  165. Address string `json:"address"`
  166. }
  167. if err := json.Unmarshal([]byte(info.node.keyJSON), &key); err == nil {
  168. report["Funding account"] = common.HexToAddress(key.Address).Hex()
  169. } else {
  170. log.Error("Failed to retrieve signer address", "err", err)
  171. }
  172. }
  173. return report
  174. }
  175. // checkFaucet does a health-check against a faucet server to verify whether
  176. // it's running, and if yes, gathering a collection of useful infos about it.
  177. func checkFaucet(client *sshClient, network string) (*faucetInfos, error) {
  178. // Inspect a possible faucet container on the host
  179. infos, err := inspectContainer(client, fmt.Sprintf("%s_faucet_1", network))
  180. if err != nil {
  181. return nil, err
  182. }
  183. if !infos.running {
  184. return nil, ErrServiceOffline
  185. }
  186. // Resolve the port from the host, or the reverse proxy
  187. port := infos.portmap["8080/tcp"]
  188. if port == 0 {
  189. if proxy, _ := checkNginx(client, network); proxy != nil {
  190. port = proxy.port
  191. }
  192. }
  193. if port == 0 {
  194. return nil, ErrNotExposed
  195. }
  196. // Resolve the host from the reverse-proxy and the config values
  197. host := infos.envvars["VIRTUAL_HOST"]
  198. if host == "" {
  199. host = client.server
  200. }
  201. amount, _ := strconv.Atoi(infos.envvars["FAUCET_AMOUNT"])
  202. minutes, _ := strconv.Atoi(infos.envvars["FAUCET_MINUTES"])
  203. tiers, _ := strconv.Atoi(infos.envvars["FAUCET_TIERS"])
  204. // Retrieve the funding account information
  205. var out []byte
  206. keyJSON, keyPass := "", ""
  207. if out, err = client.Run(fmt.Sprintf("docker exec %s_faucet_1 cat /account.json", network)); err == nil {
  208. keyJSON = string(bytes.TrimSpace(out))
  209. }
  210. if out, err = client.Run(fmt.Sprintf("docker exec %s_faucet_1 cat /account.pass", network)); err == nil {
  211. keyPass = string(bytes.TrimSpace(out))
  212. }
  213. // Run a sanity check to see if the port is reachable
  214. if err = checkPort(host, port); err != nil {
  215. log.Warn("Faucet service seems unreachable", "server", host, "port", port, "err", err)
  216. }
  217. // Container available, assemble and return the useful infos
  218. return &faucetInfos{
  219. node: &nodeInfos{
  220. datadir: infos.volumes["/root/.faucet"],
  221. port: infos.portmap[infos.envvars["ETH_PORT"]+"/tcp"],
  222. ethstats: infos.envvars["ETH_NAME"],
  223. keyJSON: keyJSON,
  224. keyPass: keyPass,
  225. },
  226. host: host,
  227. port: port,
  228. amount: amount,
  229. minutes: minutes,
  230. tiers: tiers,
  231. captchaToken: infos.envvars["CAPTCHA_TOKEN"],
  232. captchaSecret: infos.envvars["CAPTCHA_SECRET"],
  233. noauth: infos.envvars["NO_AUTH"] == "true",
  234. twitterToken: infos.envvars["TWITTER_TOKEN"],
  235. }, nil
  236. }