diff --git a/http_test.go b/http_test.go new file mode 100644 index 0000000..19ed7e2 --- /dev/null +++ b/http_test.go @@ -0,0 +1,17 @@ +package stunning + +import ( + "testing" + "fmt" + "net/http" + "log" +) + +func TestHttp(t *testing.T){ + http.HandleFunc("/", func (w http.ResponseWriter, r *http.Request) { + log.Printf("%s", r.RemoteAddr) + fmt.Fprintf(w, "Welcome to my website!") + }) + + log.Println(http.ListenAndServe(":8080", nil)) +} diff --git a/tunnel/http/http_connection.go b/tunnel/http/http_connection.go new file mode 100644 index 0000000..a1d2334 --- /dev/null +++ b/tunnel/http/http_connection.go @@ -0,0 +1,89 @@ +package http + +import ( + "time" + "net" + "log" + "net/http" + "encoding/base64" + "bytes" + "io/ioutil" +) + +type clientHttpConnection struct { + net.Conn + conn net.Conn + client http.Client + serverUrl string +} + +func (c clientHttpConnection) Read(b []byte) (n int, err error) { + req, err := http.NewRequest("GET", c.serverUrl, nil) + resp, err := c.client.Do(req) + body, _ := ioutil.ReadAll(resp.Body) + n, err = base64.StdEncoding.Decode(b, body) + log.Printf("reading bytes[%d](%v) %s -> %s", n, b) + return n, err +} + +// Write writes data to the connection. +// Write can be made to time out and return an Error with Timeout() == true +// after a fixed time limit; see SetDeadline and SetWriteDeadline. +func (c clientHttpConnection) Write(b []byte) (n int, err error) { + buff := make([]byte, 4096) + base64.StdEncoding.Encode(buff, b) + resp, err :=http.Post(c.serverUrl, "application/octet-stream", bytes.NewBuffer(buff)) + log.Printf("writing bytes(%v) %s -> %s", b, resp.Request.RemoteAddr, c.serverUrl) + return len(b), err +} + +// Close closes the connection. +// Any blocked Read or Write operations will be unblocked and return errors. +func (c clientHttpConnection) Close() error { + return c.conn.Close() +} + +// LocalAddr returns the local network address. +func (c clientHttpConnection) LocalAddr() net.Addr { + return c.conn.LocalAddr() +} + +// RemoteAddr returns the remote network address. +func (c clientHttpConnection) RemoteAddr() net.Addr { + return c.conn.RemoteAddr() +} + +// SetDeadline sets the read and write deadlines associated +// with the connection. It is equivalent to calling both +// SetReadDeadline and SetWriteDeadline. +// +// A deadline is an absolute time after which I/O operations +// fail with a timeout (see type Error) instead of +// blocking. The deadline applies to all future and pending +// I/O, not just the immediately following call to Read or +// Write. After a deadline has been exceeded, the connection +// can be refreshed by setting a deadline in the future. +// +// An idle timeout can be implemented by repeatedly extending +// the deadline after successful Read or Write calls. +// +// A zero value for t means I/O operations will not time out. +func (c clientHttpConnection) SetDeadline(t time.Time) error { + return nil +} + +// SetReadDeadline sets the deadline for future Read calls +// and any currently-blocked Read call. +// A zero value for t means Read will not time out. +func (c clientHttpConnection) SetReadDeadline(t time.Time) error { + return nil +} + +// SetWriteDeadline sets the deadline for future Write calls +// and any currently-blocked Write call. +// Even if write times out, it may return n > 0, indicating that +// some of the data was successfully written. +// A zero value for t means Write will not time out. +func (c clientHttpConnection) SetWriteDeadline(t time.Time) error { + return nil +} diff --git a/tunnel/http/http_dialer.go b/tunnel/http/http_dialer.go new file mode 100644 index 0000000..4697325 --- /dev/null +++ b/tunnel/http/http_dialer.go @@ -0,0 +1,29 @@ +package http + +import ( + tcommon "gitlab.com/h.bahadorzadeh/stunning/tunnel/common" + "golang.org/x/net/proxy" + "net" +) + +type HttpDialer struct { + tcommon.TunnelDialer + dialer proxy.Dialer +} + +func GetTcpDialer() HttpDialer { + d := HttpDialer{} + return d +} + +func (d HttpDialer) Dial(network, addr string) (c net.Conn, err error) { + conn, err := net.Dial(network, addr) + if err != nil { + return nil, err + } + return conn, err +} + +func (d HttpDialer) Protocol()tcommon.TunnelProtocol{ + return tcommon.Tcp +} diff --git a/tunnel/http/http_server.go b/tunnel/http/http_server.go new file mode 100644 index 0000000..5c98228 --- /dev/null +++ b/tunnel/http/http_server.go @@ -0,0 +1,28 @@ +package http + +import ( + "net/http" + tcommon "gitlab.com/h.bahadorzadeh/stunning/tunnel/common" + "fmt" + "net" + "log" +) + + +type TcpServer struct { + tcommon.TunnelServer +} + +func StartHttpServer(address string) (TcpServer, error) { + + http.HandleFunc("/", func (w http.ResponseWriter, r *http.Request) { + + }) + + fs := http.FileServer(http.Dir("static/")) + http.Handle("/static/", http.StripPrefix("/static/", fs)) + + http.ListenAndServe(":80", nil) + serv := TcpServer{} + return serv, nil +}