Skip to content

Commit

Permalink
Basic support for http and https proxies
Browse files Browse the repository at this point in the history
  • Loading branch information
film42 committed Oct 27, 2016
1 parent 159cc64 commit fed4050
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 2 deletions.
22 changes: 22 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

The MIT License (MIT)

Copyright (c) 2015

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
Turbulence
==========

An http proxy for navigating through rough air.
An http/https proxy for navigating through rough air.

#### Usage

```
$ go run *.go
```

```
Prepare for takeoff...
Server started on :25000
```
47 changes: 47 additions & 0 deletions connection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

import (
"bufio"
"fmt"
"net"
"net/http"
)

type connection struct {
incoming net.Conn
outgoing net.Conn
proxy proxy
}

func (c *connection) Handle() {
reader := bufio.NewReader(c.incoming)
request, err := http.ReadRequest(reader)
if err != nil {
fmt.Println("Could not parse or read request from incoming connection:", err)
return
}

if request.Method == "CONNECT" {
c.proxy = &httpsProxy{}
} else {
c.proxy = &httpProxy{}
}

c.proxy.Handle(c, request)
}

func (c *connection) Close() {
if c.incoming != nil {
c.incoming.Close()
}

if c.outgoing != nil {
c.outgoing.Close()
}
}

func NewConnection(incoming net.Conn) *connection {
return &connection{
incoming: incoming,
}
}
31 changes: 31 additions & 0 deletions http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"fmt"
"net/http"
"net/http/httputil"
)

type httpProxy struct{}

func (hp *httpProxy) Handle(connection *connection, request *http.Request) {
// Connect to outgoing host and write request bytes.
request.RequestURI = ""
client := http.Client{}
response, err := client.Do(request)
if err != nil {
fmt.Println("Error making outgoing request:", err)
return
}
defer response.Body.Close()

// Dump response struct back into bytes.
responseBytes, err := httputil.DumpResponse(response, true)
if err != nil {
fmt.Println("Error dumping response to write outgoung->incoming:", err)
return
}

// Write back to incoming connection and finish.
connection.incoming.Write(responseBytes)
}
43 changes: 43 additions & 0 deletions https.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import (
"fmt"
"io"
"net"
"net/http"
)

const ConnectionEstablished = "HTTP/1.0 200 Connection established\r\n\r\n"

type httpsProxy struct{}

func (hp *httpsProxy) Handle(connection *connection, request *http.Request) {
// Create our outgoing connection.
outgoingHost := request.URL.Host
outgoingConn, err := net.Dial("tcp", outgoingHost)
if err != nil {
fmt.Println("Error opening outgoing connection to", outgoingHost, err)
return
}
connection.outgoing = outgoingConn

// Signal to the incoming connection that the outgoing connection was established.
_, err = connection.incoming.Write([]byte(ConnectionEstablished))
if err != nil {
fmt.Println("Could not send Continue response to client: " + err.Error())
}

// Spawn incoming->outgoing and outgoing->incoming streams.
signal := make(chan bool)
go streamBytes(connection.incoming, connection.outgoing, signal)
go streamBytes(connection.outgoing, connection.incoming, signal)

// Wait for either stream to complete and finish.
<-signal
}

func streamBytes(src net.Conn, dest net.Conn, signal chan bool) {
// Read until EOF or error.
io.Copy(dest, src)
signal <- true
}
9 changes: 9 additions & 0 deletions proxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

import (
"net/http"
)

type proxy interface {
Handle(*connection, *http.Request)
}
38 changes: 37 additions & 1 deletion turbulence.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,44 @@ package main

import (
"fmt"
"net"
"os"
)

func handleConnection(conn net.Conn) {
connection := NewConnection(conn)
defer connection.Close()
connection.Handle()
}

func acceptedConnsChannel(listener net.Listener) chan net.Conn {
channel := make(chan net.Conn)
go func() {
for {
conn, err := listener.Accept()
if err != nil {
fmt.Println("Could not accept socket:", err)
continue
}

channel <- conn
}
}()
return channel
}

func main() {
fmt.Println("Prepare for take off...")
fmt.Println("Prepare for takeoff...")
server, err := net.Listen("tcp", ":25000")
if err != nil {
fmt.Println("Could not start server:", err)
os.Exit(1)
}

fmt.Println("Server started on :25000")

acceptedConnsChannel := acceptedConnsChannel(server)
for {
go handleConnection(<-acceptedConnsChannel)
}
}

0 comments on commit fed4050

Please sign in to comment.