mirror of
https://github.com/netbirdio/netbird.git
synced 2024-12-04 22:10:56 +01:00
chore: cleanup
This commit is contained in:
parent
8642b87a28
commit
0aa5eaaf5b
88
browser/client/client.go
Normal file
88
browser/client/client.go
Normal file
@ -0,0 +1,88 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"github.com/wiretrustee/wiretrustee/browser/conn"
|
||||
"github.com/wiretrustee/wiretrustee/signal/client"
|
||||
"golang.zx2c4.com/wireguard/device"
|
||||
"golang.zx2c4.com/wireguard/tun/netstack"
|
||||
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
//my private key qJi7zSrgdokeoXE27fbca2hvMlgg1NQIW6KbrTJhhmc=
|
||||
//remote private key KLuBc6tM/NRV1071bfPiNUxZmMhGBCXfxoDg+A+J7ns=
|
||||
func main() {
|
||||
|
||||
key, err := wgtypes.ParseKey("qJi7zSrgdokeoXE27fbca2hvMlgg1NQIW6KbrTJhhmc=")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
log.Printf("my public key: %s", key.PublicKey().String())
|
||||
|
||||
remoteKey, err := wgtypes.ParseKey("RFuT84MDhIvmgQndwMkxQPjG195poq713EMJZv1XPEw=")
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
|
||||
defer cancel()
|
||||
|
||||
signal, err := client.NewWebsocketClient(ctx, "ws://localhost:80/signal", key)
|
||||
|
||||
time.Sleep(5 * time.Second)
|
||||
|
||||
tun, tnet, err := netstack.CreateNetTUN(
|
||||
[]net.IP{net.ParseIP("1s00.0.2.2")},
|
||||
[]net.IP{net.ParseIP("8.8.8.8")},
|
||||
1420)
|
||||
|
||||
b := conn.NewWebRTCBind("chann-1", signal, 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=webrtc://datachannel\nallowed_ip=0.0.0.0/0",
|
||||
hex.EncodeToString(key[:]),
|
||||
hex.EncodeToString(remoteKey[:]),
|
||||
))
|
||||
|
||||
dev.Up()
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
client := http.Client{
|
||||
Transport: &http.Transport{
|
||||
DialContext: tnet.DialContext,
|
||||
},
|
||||
}
|
||||
time.Sleep(5 * time.Second)
|
||||
|
||||
//go func() {
|
||||
log.Printf("request")
|
||||
req, _ := http.NewRequest("POST", "http://100.0.2.1", bytes.NewBufferString("fdffffffffffffffffffffffffffffffdsdfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"))
|
||||
req.Header.Set("js.fetch:mode", "no-cors")
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
log.Printf(string(body))
|
||||
log.Printf(resp.Status)
|
||||
//}()
|
||||
|
||||
select {}
|
||||
|
||||
}
|
@ -1,124 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"github.com/wiretrustee/wiretrustee/browser/conn"
|
||||
"github.com/wiretrustee/wiretrustee/signal/client"
|
||||
"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"
|
||||
)
|
||||
|
||||
func handleError(err error) {
|
||||
fmt.Printf("Unexpected error. Check console.")
|
||||
panic(err)
|
||||
}
|
||||
|
||||
func getElementByID(id string) js.Value {
|
||||
return js.Global().Get("document").Call("getElementById", id)
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
|
||||
defer cancel()
|
||||
|
||||
connectToSignal := func(key wgtypes.Key, remoteKey wgtypes.Key) {
|
||||
signalClient, err := client.NewWebsocketClient(ctx, "ws://localhost:80/signal", key)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
time.Sleep(5 * time.Second)
|
||||
|
||||
tun, tnet, err := netstack.CreateNetTUN(
|
||||
[]net.IP{net.ParseIP("10.100.0.1")},
|
||||
[]net.IP{net.ParseIP("8.8.8.8")},
|
||||
1420)
|
||||
|
||||
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=25\nendpoint=webrtc://datachannel\nallowed_ip=0.0.0.0/0",
|
||||
hex.EncodeToString(key[:]),
|
||||
hex.EncodeToString(remoteKey[:]),
|
||||
))
|
||||
log.Println("4")
|
||||
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
err = dev.Up()
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
|
||||
log.Printf("device started")
|
||||
|
||||
client := http.Client{
|
||||
Transport: &http.Transport{
|
||||
DialContext: tnet.DialContext,
|
||||
},
|
||||
}
|
||||
time.Sleep(2 * time.Second)
|
||||
req, _ := http.NewRequest("GET", "https://httpbin.org/ip", nil)
|
||||
req.Header.Set("js.fetch:mode", "no-cors")
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
log.Printf(string(body))
|
||||
log.Printf(resp.Status)
|
||||
|
||||
select {}
|
||||
}
|
||||
|
||||
js.Global().Set("generateWireguardKey", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
|
||||
|
||||
key, err := wgtypes.GenerateKey()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
js.Global().Get("document").Call("getElementById", "wgPrivateKey").Set("value", key.String())
|
||||
|
||||
log.Printf("Wireguard Public key %s", key.PublicKey().String())
|
||||
js.Global().Get("document").Call("getElementById", "publicKey").Set("value", key.PublicKey().String())
|
||||
|
||||
return nil
|
||||
}))
|
||||
|
||||
js.Global().Set("connect", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
|
||||
|
||||
wgPrivateKey := js.Global().Get("document").Call("getElementById", "wgPrivateKey").Get("value").String()
|
||||
key, err := wgtypes.ParseKey(wgPrivateKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
remotePublicKey := js.Global().Get("document").Call("getElementById", "peerKey").Get("value").String()
|
||||
remoteKey, err := wgtypes.ParseKey(remotePublicKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Printf("Remote Wireguard Public key %s", remoteKey.String())
|
||||
log.Printf("Our Wireguard Public key %s", key.PublicKey().String())
|
||||
go connectToSignal(key, remoteKey)
|
||||
return nil
|
||||
}))
|
||||
|
||||
select {}
|
||||
}
|
@ -85,6 +85,7 @@ func (dc *DataChannelConn) Read(b []byte) (n int, err error) {
|
||||
}
|
||||
|
||||
func (dc *DataChannelConn) Write(b []byte) (n int, err error) {
|
||||
log.Printf("writing buffer of size %d", len(b))
|
||||
err = dc.dc.Send(b)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
|
@ -1,7 +1,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"flag"
|
||||
@ -20,12 +19,15 @@ import (
|
||||
|
||||
//my private key qJi7zSrgdokeoXE27fbca2hvMlgg1NQIW6KbrTJhhmc=
|
||||
//remote private key KLuBc6tM/NRV1071bfPiNUxZmMhGBCXfxoDg+A+J7ns=
|
||||
//./server --key KLuBc6tM/NRV1071bfPiNUxZmMhGBCXfxoDg+A+J7ns= --remote-key 6M9O7PRhKMEOiboBp9cX6rNrLBevtHX7H0O2FMXUkFI= --signal-endpoint ws://0.0.0.0:80/signal --ip 100.0.2.1 --remote-ip 100.0.2.2
|
||||
func main() {
|
||||
|
||||
keyFlag := flag.String("key", "", "a Wireguard private key")
|
||||
remoteKeyFlag := flag.String("remote-key", "", "a Wireguard remote peer public key")
|
||||
signalEndpoint := flag.String("signal-endpoint", "ws://apitest.wiretrustee.com:80/signal", "a Signal service Websocket endpoint")
|
||||
cl := flag.Bool("client", false, "indicates whether the program is a client")
|
||||
ip := flag.String("ip", "", "Wireguard IP")
|
||||
remoteIP := flag.String("remote-ip", "", "Wireguard IP")
|
||||
|
||||
flag.Parse()
|
||||
|
||||
@ -50,15 +52,20 @@ func main() {
|
||||
time.Sleep(5 * time.Second)
|
||||
|
||||
tun, tnet, err := netstack.CreateNetTUN(
|
||||
[]net.IP{net.ParseIP("10.100.0.2")},
|
||||
[]net.IP{net.ParseIP(*ip)},
|
||||
[]net.IP{net.ParseIP("8.8.8.8")},
|
||||
1420)
|
||||
|
||||
b := conn.NewWebRTCBind("chann-1", signal, 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=100\nendpoint=webrtc://datachannel\nallowed_ip=0.0.0.0/0",
|
||||
allowedIPs := *remoteIP + "/32"
|
||||
if *cl {
|
||||
allowedIPs = "0.0.0.0/0"
|
||||
}
|
||||
err = dev.IpcSet(fmt.Sprintf("private_key=%s\npublic_key=%s\npersistent_keepalive_interval=100\nendpoint=webrtc://datachannel\nallowed_ip=%s",
|
||||
hex.EncodeToString(key[:]),
|
||||
hex.EncodeToString(remoteKey[:]),
|
||||
allowedIPs,
|
||||
))
|
||||
|
||||
dev.Up()
|
||||
@ -76,7 +83,8 @@ func main() {
|
||||
|
||||
if *cl {
|
||||
|
||||
req, _ := http.NewRequest("POST", "https://httpbin.org/ip", bytes.NewBufferString("ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"))
|
||||
req, _ := http.NewRequest("GET", "http://"+*remoteIP, nil)
|
||||
|
||||
//req.Header.Set("js.fetch:mode", "no-cors")
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
@ -95,8 +103,14 @@ func main() {
|
||||
log.Panicln(err)
|
||||
}
|
||||
http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
|
||||
writer.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
writer.Header().Set("Access-Control-Allow-Headers", "Content-Type")
|
||||
if (*request).Method == "OPTIONS" {
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("> %s - %s - %s", request.RemoteAddr, request.URL.String(), request.UserAgent())
|
||||
io.WriteString(writer, "Hello from userspace TCP!")
|
||||
io.WriteString(writer, "HELOOOOOOOOOOOOOOOOOOOO")
|
||||
})
|
||||
err = http.Serve(listener, nil)
|
||||
if err != nil {
|
||||
|
60
browser/test/test.go
Normal file
60
browser/test/test.go
Normal file
@ -0,0 +1,60 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"golang.zx2c4.com/wireguard/conn"
|
||||
"golang.zx2c4.com/wireguard/device"
|
||||
"golang.zx2c4.com/wireguard/tun/netstack"
|
||||
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
||||
)
|
||||
|
||||
func main() {
|
||||
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)
|
||||
}
|
||||
|
||||
clientKey, _ := wgtypes.ParseKey("WI+uoQD9jGi+nyifmFwmswQu5r0uWFH31WeSmfU0snI=")
|
||||
publicServerkey, _ := wgtypes.ParseKey("Xp2HRQ1AJ1WbSrHV1NNHAIcmirLUjUh9jz3K3n4OcgQ=")
|
||||
fmt.Printf(clientKey.PublicKey().String())
|
||||
|
||||
dev := device.NewDevice(tun, conn.NewDefaultBind(), device.NewLogger(device.LogLevelVerbose, ""))
|
||||
|
||||
err = dev.IpcSet(fmt.Sprintf("private_key=%s\npublic_key=%s\npersistent_keepalive_interval=1\nendpoint=65.21.255.241:51820\nallowed_ip=0.0.0.0/0",
|
||||
hex.EncodeToString(clientKey[:]),
|
||||
hex.EncodeToString(publicServerkey[:]),
|
||||
))
|
||||
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://httpbin.org/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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user