-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
127 lines (112 loc) · 2.92 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
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"flag"
"fmt"
"net/http"
"os"
"strings"
"github.com/assetcloud/chain/common/log"
"github.com/assetcloud/chain/pluginmgr"
"github.com/assetcloud/plugin/plugin/dapp/cross2eth/ebcli/buildflags"
relayerTypes "github.com/assetcloud/plugin/plugin/dapp/cross2eth/ebrelayer/types"
"github.com/assetcloud/plugin/plugin/dapp/cross2eth/ebrelayer/version"
pluginVersion "github.com/assetcloud/plugin/version"
tml "github.com/BurntSushi/toml"
"github.com/spf13/cobra"
)
var (
rootCmd = &cobra.Command{
Use: "chainxEth-relayer" + "-cli",
Short: "chainxEth-relayer" + "client tools",
}
configPath = flag.String("f", "", "configfile")
)
func init() {
rootCmd.AddCommand(
SetPwdCmd(),
ChangePwdCmd(),
LockCmd(),
UnlockCmd(),
ChainRelayerCmd(),
EthereumRelayerCmd(),
StaticsCmd(),
VersionCmd(),
)
}
func testTLS(RPCAddr string) string {
rpcaddr := RPCAddr
if strings.HasPrefix(rpcaddr, "https://") {
return RPCAddr
}
if !strings.HasPrefix(rpcaddr, "http://") {
return RPCAddr
}
//test tls ok
if rpcaddr[len(rpcaddr)-1] != '/' {
rpcaddr += "/"
}
rpcaddr += "test"
resp, err := http.Get(rpcaddr)
if err != nil {
return "https://" + RPCAddr[7:]
}
defer resp.Body.Close()
if resp.StatusCode == 200 {
return RPCAddr
}
return "https://" + RPCAddr[7:]
}
//run :
func run(RPCAddr, NodeAddr string) {
//test tls is enable
RPCAddr = testTLS(RPCAddr)
pluginmgr.AddCmd(rootCmd)
log.SetLogLevel("error")
rootCmd.PersistentFlags().String("rpc_laddr", RPCAddr, "http url")
rootCmd.PersistentFlags().String("node_addr", NodeAddr, "bsc node url")
rootCmd.PersistentFlags().String("eth_chain_name", "Binance", "bsc chain name")
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func initCfg(path string) *relayerTypes.RelayerConfig {
var cfg relayerTypes.RelayerConfig
if _, err := tml.DecodeFile(path, &cfg); err != nil {
fmt.Println(err)
os.Exit(-1)
}
return &cfg
}
func main() {
if *configPath == "" {
*configPath = "relayer.toml"
}
cfg := initCfg(*configPath)
if buildflags.RPCAddr == "" {
buildflags.RPCAddr = "http://localhost:9901"
}
if buildflags.NodeAddr == "" {
buildflags.NodeAddr = cfg.EthRelayerCfg[0].EthProviderCli[0]
}
run(buildflags.RPCAddr, buildflags.NodeAddr)
}
func VersionCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "version",
Short: "show version",
Run: showVersion,
}
return cmd
}
func showVersion(_ *cobra.Command, _ []string) {
fmt.Println("plugin version :", pluginVersion.GetVersion())
fmt.Println("relayer version :", version.GetVersion())
fmt.Println("commit :", version.GitCommit)
fmt.Println("buildTime :", version.BuildTime)
fmt.Println("goVersion :", version.GoVersion)
fmt.Println("platform :", version.Platform)
}