-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
104 lines (87 loc) · 2.25 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
package main
import (
"crypto/ecdsa"
"flag"
"fmt"
"math/big"
"sync"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/discover"
)
const ua = "manspreading"
const ver = "1.0.0"
// statusData is the network packet for the status message.
type statusData struct {
ProtocolVersion uint32
NetworkId uint64
TD *big.Int
CurrentBlock common.Hash
GenesisBlock common.Hash
}
// newBlockData is the network packet for the block propagation message.
type newBlockData struct {
Block *types.Block
TD *big.Int
}
type conn struct {
p *p2p.Peer
rw p2p.MsgReadWriter
}
type proxy struct {
lock sync.RWMutex
upstreamNode *discover.Node
upstreamConn *conn
downstreamConn *conn
upstreamState statusData
srv *p2p.Server
}
var pxy *proxy
var upstreamUrl = flag.String("upstream", "", "upstream enode url to connect to")
var listenAddr = flag.String("listenaddr", "127.0.0.1:36666", "listening addr")
var privkey = flag.String("nodekey", "", "nodekey file")
func init() {
flag.Parse()
}
func main() {
var nodekey *ecdsa.PrivateKey
if *privkey != "" {
nodekey, _ = crypto.LoadECDSA(*privkey)
fmt.Println("Node Key loaded from ", *privkey)
} else {
nodekey, _ = crypto.GenerateKey()
crypto.SaveECDSA("./nodekey", nodekey)
fmt.Println("Node Key generated and saved to ./nodekey")
}
node, _ := discover.ParseNode(*upstreamUrl)
pxy = &proxy{
upstreamNode: node,
}
config := p2p.Config{
PrivateKey: nodekey,
MaxPeers: 2,
NoDiscovery: true,
DiscoveryV5: false,
Name: common.MakeName(fmt.Sprintf("%s/%s", ua, node.ID.String()), ver),
BootstrapNodes: []*discover.Node{node},
StaticNodes: []*discover.Node{node},
TrustedNodes: []*discover.Node{node},
Protocols: []p2p.Protocol{newManspreadingProtocol()},
ListenAddr: *listenAddr,
Logger: log.New(),
}
config.Logger.SetHandler(log.StdoutHandler)
pxy.srv = &p2p.Server{Config: config}
// Wait forever
var wg sync.WaitGroup
wg.Add(2)
err := pxy.srv.Start()
wg.Done()
if err != nil {
fmt.Println(err)
}
wg.Wait()
}