mirror of
https://github.com/netbirdio/netbird.git
synced 2026-04-20 01:06:45 +00:00
feature: successful wireguard connection
This commit is contained in:
Binary file not shown.
@@ -1,39 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
conn2 "golang.zx2c4.com/wireguard/conn"
|
||||
"golang.zx2c4.com/wireguard/device"
|
||||
"golang.zx2c4.com/wireguard/tun/netstack"
|
||||
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
||||
"net"
|
||||
)
|
||||
|
||||
func main() {
|
||||
tun, _, err := netstack.CreateNetTUN(
|
||||
[]net.IP{net.ParseIP("10.100.0.2")},
|
||||
[]net.IP{net.ParseIP("8.8.8.8")},
|
||||
1420)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
clientKey, _ := wgtypes.ParseKey("WI+uoQD9jGi+nyifmFwmswQu5r0uWFH31WeSmfU0snI=")
|
||||
serverKey, _ := wgtypes.ParseKey("kLpbgt+g2+g8x556VmsLYyhTh77WmKfaFB0x+LcVyWY=")
|
||||
publicServerkey := serverKey.PublicKey()
|
||||
|
||||
dev := device.NewDevice(tun, conn2.NewStdNetBind(), device.NewLogger(device.LogLevelVerbose, ""))
|
||||
|
||||
err = dev.IpcSet(fmt.Sprintf("private_key=%s\npublic_key=%s\npersistent_keepalive_interval=5\nendpoint=65.108.52.126:50000\nallowed_ip=0.0.0.0/0",
|
||||
hex.EncodeToString(clientKey[:]),
|
||||
hex.EncodeToString(publicServerkey[:]),
|
||||
))
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
select {}
|
||||
}
|
||||
@@ -16,6 +16,7 @@ import (
|
||||
|
||||
func (*WebRTCBind) makeReceive(dcConn net.Conn) conn.ReceiveFunc {
|
||||
return func(buff []byte) (int, conn.Endpoint, error) {
|
||||
log.Printf("receiving from endpoint %s", dcConn.RemoteAddr().String())
|
||||
n, err := dcConn.Read(buff)
|
||||
if err != nil {
|
||||
return 0, nil, err
|
||||
@@ -260,132 +261,6 @@ func (bind *WebRTCBind) Open(port uint16) (fns []conn.ReceiveFunc, actualPort ui
|
||||
|
||||
}
|
||||
|
||||
/*func (bind *WebRTCBind) Open(port uint16) (fns []conn.ReceiveFunc, actualPort uint16, err error) {
|
||||
|
||||
log.Printf("OPEN 1------")
|
||||
|
||||
controlling := bind.key > bind.remoteKey
|
||||
|
||||
bind.mu.Lock()
|
||||
defer bind.mu.Unlock()
|
||||
|
||||
config := webrtc.Configuration{
|
||||
ICEServers: []webrtc.ICEServer{
|
||||
{
|
||||
URLs: []string{"stun:stun.l.google.com:19302"},
|
||||
},
|
||||
},
|
||||
}
|
||||
pc, err := webrtc.NewPeerConnection(config)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
bind.pc = pc
|
||||
|
||||
log.Printf("OPEN 2------")
|
||||
|
||||
log.Printf("OPEN 3------")
|
||||
|
||||
var sdp webrtc.SessionDescription
|
||||
var dc *webrtc.DataChannel
|
||||
if controlling {
|
||||
// Create offer
|
||||
sdp, err = pc.CreateOffer(nil)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
if err := pc.SetLocalDescription(sdp); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
dc, err = pc.CreateDataChannel(bind.id, nil)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
} else {
|
||||
dcConn, err := WrapDataChannel(dc)
|
||||
if err != nil {
|
||||
dc.Close()
|
||||
return nil, 0, err
|
||||
}
|
||||
}
|
||||
|
||||
go bind.signal.Receive(func(msg *proto.Message) error {
|
||||
log.Printf("received a message from %v -> %v", msg.RemoteKey, msg.Body.Payload)
|
||||
switch msg.GetBody().Type {
|
||||
case proto.Body_OFFER:
|
||||
|
||||
log.Printf("received offer %s", msg.GetBody().GetPayload())
|
||||
|
||||
err = setRemoteDescription(pc, msg.GetBody().GetPayload())
|
||||
if err != nil {
|
||||
log.Printf("%v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
sdp, err := pc.CreateAnswer(nil)
|
||||
if err != nil {
|
||||
log.Printf("%v", err)
|
||||
return err
|
||||
}
|
||||
if err := pc.SetLocalDescription(sdp); err != nil {
|
||||
log.Printf("%v", err)
|
||||
return err
|
||||
}
|
||||
break
|
||||
case proto.Body_ANSWER:
|
||||
|
||||
log.Printf("received answer %s", msg.GetBody().GetPayload())
|
||||
|
||||
err = setRemoteDescription(pc, msg.GetBody().GetPayload())
|
||||
if err != nil {
|
||||
log.Printf("%v", err)
|
||||
return err
|
||||
}
|
||||
break
|
||||
case proto.Body_CANDIDATE:
|
||||
log.Printf("received candidate %s", msg.GetBody().GetPayload())
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
// Add handlers for setting up the connection.
|
||||
pc.OnICEConnectionStateChange(func(state webrtc.ICEConnectionState) {
|
||||
fmt.Println(fmt.Sprint(state))
|
||||
})
|
||||
pc.OnICECandidate(func(candidate *webrtc.ICECandidate) {
|
||||
if candidate != nil {
|
||||
if controlling {
|
||||
bind.signal.Send(&proto.Message{
|
||||
Key: bind.key,
|
||||
RemoteKey: bind.remoteKey,
|
||||
Body: &proto.Body{
|
||||
Type: proto.Body_OFFER,
|
||||
Payload: Encode(pc.LocalDescription()),
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
log.Printf("OPEN 4------")
|
||||
|
||||
// blocks until channel is open
|
||||
dcConn, err := WrapDataChannel(dc)
|
||||
if err != nil {
|
||||
dc.Close()
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
bind.conn = dcConn
|
||||
|
||||
fns = append(fns, bind.makeReceive(bind.conn))
|
||||
|
||||
log.Printf("OPEN 5------")
|
||||
|
||||
return fns, 38676, nil
|
||||
}*/
|
||||
|
||||
func setRemoteDescription(pc *webrtc.PeerConnection, payload string) error {
|
||||
descr, err := Decode(payload)
|
||||
if err != nil {
|
||||
@@ -436,27 +311,32 @@ func (bind *WebRTCBind) Send(b []byte, ep conn.Endpoint) error {
|
||||
}
|
||||
|
||||
func (*WebRTCBind) ParseEndpoint(s string) (conn.Endpoint, error) {
|
||||
return nil, nil
|
||||
log.Printf("peer endpoint %s", s)
|
||||
return &WebRTCEndpoint{}, nil
|
||||
}
|
||||
|
||||
// WebRTCEndpoint is an implementation of Wireguard's Endpoint interface backed by WebRTC
|
||||
type WebRTCEndpoint DataChannelAddr
|
||||
|
||||
func (*WebRTCEndpoint) ClearSrc() {
|
||||
func (e *WebRTCEndpoint) ClearSrc() {
|
||||
|
||||
}
|
||||
func (*WebRTCEndpoint) SrcToString() string {
|
||||
func (e *WebRTCEndpoint) SrcToString() string {
|
||||
return ""
|
||||
}
|
||||
func (*WebRTCEndpoint) DstToString() string {
|
||||
return ""
|
||||
func (e *WebRTCEndpoint) DstToString() string {
|
||||
return (*DataChannelAddr)(e).String()
|
||||
}
|
||||
func (*WebRTCEndpoint) DstToBytes() []byte {
|
||||
return nil
|
||||
}
|
||||
func (*WebRTCEndpoint) DstIP() net.IP {
|
||||
return nil
|
||||
}
|
||||
func (*WebRTCEndpoint) SrcIP() net.IP {
|
||||
func (e *WebRTCEndpoint) DstToBytes() []byte {
|
||||
port := 31234
|
||||
out := net.IP{127, 0, 0, 1}
|
||||
out = append(out, byte(port&0xff))
|
||||
out = append(out, byte((port>>8)&0xff))
|
||||
return out
|
||||
}
|
||||
func (e *WebRTCEndpoint) DstIP() net.IP {
|
||||
return net.IP{127, 0, 0, 1}
|
||||
}
|
||||
func (e *WebRTCEndpoint) SrcIP() net.IP {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -9,8 +9,10 @@ import (
|
||||
"golang.zx2c4.com/wireguard/device"
|
||||
"golang.zx2c4.com/wireguard/tun/netstack"
|
||||
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"syscall/js"
|
||||
"time"
|
||||
)
|
||||
@@ -37,9 +39,7 @@ func main() {
|
||||
|
||||
time.Sleep(5 * time.Second)
|
||||
|
||||
log.Printf("connected to signal")
|
||||
|
||||
tun, _, err := netstack.CreateNetTUN(
|
||||
tun, tnet, err := netstack.CreateNetTUN(
|
||||
[]net.IP{net.ParseIP("10.100.0.2")},
|
||||
[]net.IP{net.ParseIP("8.8.8.8")},
|
||||
1420)
|
||||
@@ -47,7 +47,7 @@ func main() {
|
||||
b := conn.NewWebRTCBind("chann-1", signalClient, key.PublicKey().String(), remoteKey.String())
|
||||
dev := device.NewDevice(tun, b, device.NewLogger(device.LogLevelVerbose, ""))
|
||||
|
||||
err = dev.IpcSet(fmt.Sprintf("private_key=%s\npublic_key=%s\npersistent_keepalive_interval=5\nendpoint=65.108.52.126:50000\nallowed_ip=0.0.0.0/0",
|
||||
err = dev.IpcSet(fmt.Sprintf("private_key=%s\npublic_key=%s\npersistent_keepalive_interval=10\nendpoint=webrtc://datachannel\nallowed_ip=0.0.0.0/0",
|
||||
hex.EncodeToString(key[:]),
|
||||
hex.EncodeToString(remoteKey[:]),
|
||||
))
|
||||
@@ -63,6 +63,21 @@ func main() {
|
||||
|
||||
log.Printf("device started")
|
||||
|
||||
client := http.Client{
|
||||
Transport: &http.Transport{
|
||||
DialContext: tnet.DialContext,
|
||||
},
|
||||
}
|
||||
resp, err := client.Get("http://localhost:9090/")
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
log.Println(string(body))
|
||||
|
||||
select {}
|
||||
}
|
||||
|
||||
@@ -102,52 +117,4 @@ func main() {
|
||||
}))
|
||||
|
||||
select {}
|
||||
|
||||
/*tun, tnet, err := netstack.CreateNetTUN(
|
||||
[]net.IP{net.ParseIP("10.100.0.2")},
|
||||
[]net.IP{net.ParseIP("8.8.8.8")},
|
||||
1420)
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
log.Println("1")
|
||||
clientKey,_ := wgtypes.ParseKey("WI+uoQD9jGi+nyifmFwmswQu5r0uWFH31WeSmfU0snI=")
|
||||
serverKey,_ := wgtypes.ParseKey("kLpbgt+g2+g8x556VmsLYyhTh77WmKfaFB0x+LcVyWY=")
|
||||
publicServerkey := serverKey.PublicKey()
|
||||
log.Println("2")*/
|
||||
|
||||
/*/*
|
||||
|
||||
|
||||
dev := device.NewDevice(tun, conn.NewDefaultBind(), device.NewLogger(device.LogLevelVerbose, ""))
|
||||
|
||||
err = dev.IpcSet(fmt.Sprintf("private_key=%s\npublic_key=%s\npersistent_keepalive_interval=5\nendpoint=65.108.52.126:50000\nallowed_ip=0.0.0.0/0",
|
||||
hex.EncodeToString(clientKey[:]),
|
||||
hex.EncodeToString(publicServerkey[:]),
|
||||
))
|
||||
log.Println("4")
|
||||
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
err = dev.Up()
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
|
||||
client := http.Client{
|
||||
Transport: &http.Transport{
|
||||
DialContext: tnet.DialContext,
|
||||
},
|
||||
}
|
||||
resp, err := client.Get("https://www.zx2c4.com/ip")
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
log.Println(string(body))
|
||||
time.Sleep(30 * time.Second)*/
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user