forked from omgupta-JUS/Relay-Torrentium
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
154 lines (128 loc) · 4.29 KB
/
Copy pathmain.go
File metadata and controls
154 lines (128 loc) · 4.29 KB
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
147
148
149
150
151
152
153
154
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"log"
"os"
libp2p "github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/network"
relayv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay"
noise "github.com/libp2p/go-libp2p/p2p/security/noise"
ws "github.com/libp2p/go-libp2p/p2p/transport/websocket"
ma "github.com/multiformats/go-multiaddr"
// priv "privkey/privkey"
)
func loadandgenratepriv() *rsa.PrivateKey {
// generating a new RSA private key of 2048 bits
const pemFile = "private_key.pem"
var privatekey *rsa.PrivateKey
if _, err := os.Stat(pemFile); err == nil {
data, err := os.ReadFile(pemFile)
if err != nil {
log.Println("couldnt read the file", err)
}
block, _ := pem.Decode(data)
if block == nil {
log.Printf("pemfile not decoded")
}
privkey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
log.Fatalf("Failed to parse RSA private key: %v", err)
}
return privkey
}
privatekey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
log.Printf("error in creating rsa private key: %v", err)
}
// encoding private key to PEM format
// header label of PEM file that will appear in file
privateKeyPEM := &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(privatekey),
}
privateKeyFile, err := os.Create(pemFile)
if err != nil {
log.Println("Error creating private key file:", err)
}
pem.Encode(privateKeyFile, privateKeyPEM)
privateKeyFile.Close()
return privatekey
}
type NotifyBundle struct{}
// if relay start listening on multiaddr
func (nb *NotifyBundle) Listen(n network.Network, a ma.Multiaddr) {
log.Printf("[notifiee] Listen: %s\n", a)
}
// if relay stops listening on multiaddr
func (nb *NotifyBundle) ListenClose(n network.Network, a ma.Multiaddr) {
log.Printf("[notifiee] ListenClose: %s\n", a)
}
// if someone connects on this multiaddr
func (nb *NotifyBundle) Connected(n network.Network, c network.Conn) {
log.Printf("[notifiee] Connected: %s <-> %s peer=%s\n",
c.LocalMultiaddr(), c.RemoteMultiaddr(), c.RemotePeer().String())
}
func (nb *NotifyBundle) Disconnected(n network.Network, c network.Conn) {
log.Printf("[notifiee] Disconnected: %s <-> %s peer=%s\n",
c.LocalMultiaddr(), c.RemoteMultiaddr(), c.RemotePeer().String())
}
// OpenedStream is called when a stream is opened on a connection
func (nb *NotifyBundle) OpenedStream(net network.Network, stream network.Stream) {
log.Printf("[notifiee] OpenedStream: from=%s to=%s protocol=%s\n",
stream.Conn().LocalPeer().String(), stream.Conn().RemotePeer().String(), stream.Protocol())
}
// ClosedStream is called when a stream is closed
func (nb *NotifyBundle) ClosedStream(net network.Network, stream network.Stream) {
log.Printf("[notifiee] ClosedStream: from=%s to=%s protocol=%s\n",
stream.Conn().LocalPeer().String(), stream.Conn().RemotePeer().String(), stream.Protocol())
}
func main() {
log.Println(" Starting libp2p relay...")
publicDNS := os.Getenv("RENDER_EXTERNAL_URL")
port := os.Getenv("PORT")
if port == "" {
port = "443"
}
privkey := loadandgenratepriv()
// using DER(DISTINGUISHED ENCODING RULES) FOR RSA->crypto
// Convert to PKCS1 DER bytes
der := x509.MarshalPKCS1PrivateKey(privkey)
// Now turn it into libp2p's crypto.PrivKey
priv, err := crypto.UnmarshalRsaPrivateKey(der)
if err != nil {
log.Printf("unable to convert to libp2p format %v", err)
}
// using a layer of noise protocol for secured connection between peer and relay
h, err := libp2p.New(
libp2p.Identity(priv),
libp2p.Security(noise.ID, noise.New),
libp2p.Transport(ws.New),
libp2p.AddrsFactory(func(addrs []ma.Multiaddr) []ma.Multiaddr {
maddr, _ := ma.NewMultiaddr(
fmt.Sprintf("/dns4/%s/tcp/%s/wss", publicDNS, port),
)
return []ma.Multiaddr{maddr}
}),
)
if err != nil {
log.Printf("Failed to create libp2p host: %v", err)
}
// relay as a libp2p host
_, err = relayv2.New(h)
if err != nil {
log.Printf(" Failed to enable relay v2: %v", err)
}
// Register the notifiee so we get connection/stream events
h.Network().Notify(&NotifyBundle{})
log.Println("Relay started successfully")
log.Printf(" Peer ID: %s", h.ID())
for _, addr := range h.Addrs() {
log.Printf(" Listening on: %s/p2p/%s", addr, h.ID())
}
select {}
}