Skip to content

Commit

Permalink
stabilisation
Browse files Browse the repository at this point in the history
staaldraad committed Oct 28, 2020

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
1 parent 7a0cef8 commit 48f082c
Showing 3 changed files with 52 additions and 49 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -10,4 +10,4 @@ require (

replace gortc.io/turn => github.com/staaldraad/turn v0.11.3

replace gortc.io/turnc => github.com/staaldraad/turnc v0.2.5
replace gortc.io/turnc => github.com/staaldraad/turnc v0.2.7
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -7,6 +7,8 @@ github.com/staaldraad/turnc v0.2.4 h1:XEdB8BlwHWr6No1juexZeFm7Qxsz1FfCl9kg9JbNjS
github.com/staaldraad/turnc v0.2.4/go.mod h1:OnT2WcZz7g2oZGmYAslnEgaJF4fNmexoEO7n6Cp84GI=
github.com/staaldraad/turnc v0.2.5 h1:UWuiq7Yp3YuktdtBHhBcYPPl5Uu47ap6tZbfi6kK04M=
github.com/staaldraad/turnc v0.2.5/go.mod h1:OnT2WcZz7g2oZGmYAslnEgaJF4fNmexoEO7n6Cp84GI=
github.com/staaldraad/turnc v0.2.7 h1:+GiyYcJB6Nwsh8OJlJfZyAzNhmrnYuWYWmTBwO0b7xA=
github.com/staaldraad/turnc v0.2.7/go.mod h1:OnT2WcZz7g2oZGmYAslnEgaJF4fNmexoEO7n6Cp84GI=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
go.uber.org/atomic v1.4.0 h1:cxzIVoETapQEqDhQu3QfnvXAV4AlzcvUCxkVUFw3+EU=
97 changes: 49 additions & 48 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"crypto/tls"
"encoding/binary"
"flag"
"fmt"
@@ -76,7 +75,7 @@ func handleHTTP(w http.ResponseWriter, r *http.Request) {

var closer sync.Once

controlConn, destConn, err := connectTurn(peer)
controlConn, destConn, client, clientb, err := connectTurn(peer)
if err != nil {
if controlConn != nil {
controlConn.Close()
@@ -107,6 +106,8 @@ func handleHTTP(w http.ResponseWriter, r *http.Request) {
_ = controlConn.Close()
_ = destConn.Close()
_ = conn.Close()
_ = client.Close()
_ = clientb.Close()
}

//ugly hack to recreate same function that could be achieved with httputil.DumpRequest
@@ -135,53 +136,38 @@ func handleHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Printf("%q\n", dump)
*/

//destConn.SetReadBuffer(512)
destConn.SetReadBuffer(512)

timeoutDuration := 5 * time.Second
//bufReader := bufio.NewReader(destConn)
//buf := make([]byte, 2048)
// Set a deadline for reading. Read operation will fail if no data
// is received after deadline.
destConn.SetReadDeadline(time.Now().Add(timeoutDuration))

for {
// Set a deadline for reading. Read operation will fail if no data
// is received after deadline.
destConn.SetReadDeadline(time.Now().Add(timeoutDuration))

_, err := io.Copy(bufwr, destConn)
if err != nil {
if err == io.EOF {
continue
}
break
}
/*
// Read tokens delimited by newline
bytes, err := destConn.Read()
if err != nil {
fmt.Println(err)
break
}
*/
//fmt.Printf("%s", bytes)
//bufwr.Write(buf[:n])
//bufwr.Flush()
}
// close the connections
closer.Do(closeFunc)

}

func transfer(destination io.WriteCloser, source io.ReadCloser, closer *sync.Once) {
func transfer(destination io.WriteCloser, source io.ReadCloser, c, cb *turnc.Client, closer *sync.Once) {
closeFunc := func() {
fmt.Println("[*] Connections closed.")
_ = destination.Close()
_ = source.Close()
_ = c.Close()
_ = cb.Close()
}
io.Copy(destination, source)
closer.Do(closeFunc)
}

func handleProxyTun(w http.ResponseWriter, r *http.Request) {
fmt.Println("CONNECT")

target := r.URL.Host
if target == "" {
@@ -210,36 +196,39 @@ func handleProxyTun(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusServiceUnavailable)
}

controlConn, destConn, err := connectTurn(peer)
controlConn, destConn, client, clientb, err := connectTurn(peer)
if err != nil {
if controlConn != nil {
controlConn.Close()
}
if destConn != nil {
destConn.Close()
}
clientConn.Write([]byte("Proxy encountered error"))
w.WriteHeader(http.StatusInternalServerError)
return
//clientConn.Write([]byte("Proxy encountered error"))
}

// make sure connections get closed, use a sync.Once to ensure the close happens in one of the handlers
var closer sync.Once

go transfer(destConn, clientConn, &closer)
transfer(clientConn, destConn, &closer)
go transfer(destConn, clientConn, client, clientb, &closer)
transfer(clientConn, destConn, client, clientb, &closer)

clientConn.Close()
}

func connectTurn(target string) (*net.TCPConn, *net.TCPConn, error) {
func connectTurn(target string) (*net.TCPConn, *net.TCPConn, *turnc.Client, *turnc.Client, error) {
// Resolving to TURN server.
raddr, err := net.ResolveTCPAddr("tcp", *server)
if err != nil {
fmt.Println(err)
return nil, nil, err
return nil, nil, nil, nil, err
}
c, err := net.DialTCP("tcp", nil, raddr)
if err != nil {
fmt.Println(err)
return nil, nil, err
return nil, nil, nil, nil, err
}
fmt.Printf("[*] Dial server %s -> %s\n", c.LocalAddr(), c.RemoteAddr())
client, clientErr := turnc.New(turnc.Options{
@@ -249,67 +238,79 @@ func connectTurn(target string) (*net.TCPConn, *net.TCPConn, error) {
})
if clientErr != nil {
fmt.Println(clientErr)
return c, nil, err
return c, nil, nil, nil, err
}
//client.StartRead()
a, allocErr := client.AllocateTCP()
if allocErr != nil {
fmt.Println(allocErr)
return c, nil, err
return c, nil, nil, nil, err
}
peerAddr, resolveErr := net.ResolveTCPAddr("tcp", target)
if resolveErr != nil {
fmt.Println(resolveErr)
return c, nil, err
return c, nil, nil, nil, err
}
fmt.Println("[*] Create peer permission")
permission, createErr := a.Create(peerAddr.IP)
if createErr != nil {
fmt.Println(createErr)
return c, nil, err
return c, nil, nil, nil, err
}
fmt.Println("[*] Create TCP Session Connection")
conn, err := permission.CreateTCP(peerAddr)
if err != nil {
fmt.Println(err)
return c, nil, err
return c, nil, nil, nil, err
}

fmt.Println("[*] Create connect request")
var connid stun.RawAttribute
if connid, err = conn.Connect(); err != nil {
fmt.Println(err)
return c, nil, err
return c, nil, nil, nil, err
}

// setup bind
fmt.Println("[*] Create bind TCP connection")
cb, err := net.DialTCP("tcp", nil, raddr)
if err != nil {
fmt.Println(err)
return c, nil, err
return c, nil, nil, nil, err
}

fmt.Println("[*] Auth and Create client ")
clientb, clientErr := turnc.New(turnc.Options{
Conn: cb,
Username: *username,
Password: *password,
Conn: cb,
})
if clientErr != nil {
fmt.Println(clientErr)
return c, cb, err
return c, nil, client, clientb, err
}

time.Sleep(500 * time.Millisecond)

fmt.Println("[*] Bind client ")
err = clientb.ConnectionBind(turn.ConnectionID(binary.BigEndian.Uint32(connid.Value)), a)
_, err = clientb.ConnectionBind(turn.ConnectionID(binary.BigEndian.Uint32(connid.Value)), a)
if err != nil {
fmt.Println("[x] Couldn't bind", err)
return c, cb, err
return c, cb, client, clientb, err
}
//clientb.StartRead()

fmt.Println("[*] Bound")
return c, cb, nil
return c, cb, client, clientb, nil
}

func readControl(conn *turnc.Connection, connectionAttempt chan bool) {
buf := make([]byte, 512)
for {
n, err := conn.Read(buf)
if err != nil {
fmt.Println(err)
break
}
fmt.Printf("\n%x\n", buf[:n])
}
conn.Close()
}

func main() {
@@ -325,7 +326,7 @@ func main() {
}
}),
// Disable HTTP/2.
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)),
//TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)),
}

log.Fatal(server.ListenAndServe())

0 comments on commit 48f082c

Please sign in to comment.