This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
146 lines (138 loc) · 3.65 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
_ "net/http/pprof"
"os"
"time"
"github.com/govm-net/govm/api"
"github.com/govm-net/govm/conf"
"github.com/govm-net/govm/database"
"github.com/govm-net/govm/handler"
"github.com/govm-net/govm/wallet"
"github.com/lengzhao/libp2p/crypto"
"github.com/lengzhao/libp2p/network"
"github.com/lengzhao/libp2p/plugins"
"gopkg.in/natefinch/lumberjack.v2"
)
func main() {
log.SetFlags(log.Lshortfile | log.LstdFlags)
c := conf.GetConf()
log.Printf("software version:%s,build time:%s,git head:%s",
conf.Version, conf.BuildTime, conf.GitHead)
if c.SaveLog {
log.SetOutput(&lumberjack.Logger{
Filename: "./log/govm.log",
MaxSize: 50, // megabytes
MaxBackups: 5,
MaxAge: 10, //days
Compress: true, // disabled by default
})
} else {
log.SetOutput(ioutil.Discard)
}
database.ChangeClientNumber(10)
client := database.GetClient()
val := client.Get(1, []byte("info"), []byte("net"))
if len(val) == 0 {
err := client.Set(1, []byte("info"), []byte("net"), []byte(c.NetID))
if err != nil {
log.Println("fail to set database,make sure the database server running.", err)
fmt.Println("fail to set database,make sure the database server running.", err)
os.Exit(2)
}
} else if string(val) != c.NetID {
log.Println("different net id,hope:", c.NetID, ", get:", val)
fmt.Println("different net id,hope:", c.NetID, ", get:", val)
os.Exit(3)
}
conf.LoadWallet(c.WalletFile, c.Password)
// startHTTPServer
{
addr := fmt.Sprintf(":%d", c.HTTPPort)
router := api.NewRouter()
go func() {
if c.TLSCertFile != "" && c.TLSKeyFile != "" {
if _, err := os.Stat(c.TLSCertFile); !os.IsNotExist(err) {
err := http.ListenAndServeTLS(addr, c.TLSCertFile,
c.TLSKeyFile, router)
if err != nil {
log.Println("https error:", err)
os.Exit(2)
}
return
}
}
err := http.ListenAndServe(addr, router)
if err != nil {
log.Println("fail to http Listen:", addr, err)
os.Exit(2)
}
}()
if c.PrivateAddress != "" {
go func() {
err := http.ListenAndServe(c.PrivateAddress, api.NewPrivateRouter())
if err != nil {
log.Println("fail to http Listen:", addr, err)
os.Exit(2)
}
}()
}
}
n := network.New()
if n == nil {
log.Println("fail to new network")
os.Exit(2)
}
{
data, err := ioutil.ReadFile("./conf/bootstrap.json")
if err == nil {
var peers []string
err = json.Unmarshal(data, &peers)
if err == nil {
b := new(plugins.Bootstrap)
b.Addrs = peers
n.RegistPlugin(b)
}
}
}
n.RegistPlugin(new(plugins.DiscoveryPlugin))
n.RegistPlugin(new(plugins.Broadcast))
key := loadNodeKey()
rk := wallet.EcdsaKey{Type: c.NetID, NeedVerify: c.VerifyNetData}
cp := crypto.NewMgr()
cp.Register(&rk)
cp.SetPrivKey(rk.GetType(), key)
n.SetKeyMgr(cp)
n.RegistPlugin(new(handler.MsgPlugin))
n.RegistPlugin(new(handler.InternalPlugin))
n.RegistPlugin(new(handler.SyncPlugin))
n.RegistPlugin(new(handler.NATTPlugin))
err := n.Listen(c.ServerHost)
if err != nil {
log.Println("fail to listen:", c.ServerHost, err)
}
n.Close()
log.Println("wait to exit(3s)")
time.Sleep(3 * time.Second)
}
func loadNodeKey() []byte {
if !conf.GetConf().SaveNodeInfo {
return wallet.NewPrivateKey()
}
const (
nodeKeyFile = "./conf/node_key.dat"
passwd = "10293847561029384756"
)
w, err := wallet.LoadWallet(nodeKeyFile, passwd)
if err != nil {
w.Key = wallet.NewPrivateKey()
pubKey := wallet.GetPublicKey(w.Key, wallet.EAddrTypeDefault)
addr := wallet.PublicKeyToAddress(pubKey, wallet.EAddrTypeDefault)
wallet.SaveWallet(nodeKeyFile, passwd, addr, w.Key, nil)
}
return w.Key
}