-1

I was writing a proxifier like app for Linux in golang. The app when executed will listen for all the TCP connections and redirect them to the proxy server address. In between the app also adds a "Proxy-Authorisation : Basic ..." header to the HTTP Headers. When I saw the TCP headers I was unable to get the HTTP Headers. Where am I going wrong or How can I extract the HTTP Data? Is there any other way to achieve this?

Love_for_CODE
  • 197
  • 1
  • 10
  • 3
    The main problem is that a TCP connection doesn't knows about HTTP. You should probably use the `net/http.ReadRequest` method to parse the HTTP request, which should give you access to the HTTP headers. See [the doc](https://golang.org/pkg/net/http/#ReadRequest). – Elwinar Dec 30 '15 at 13:44
  • I don't know much about golang, but in c/c++ i can read socket fd using read() system call and i get head/content of http request. – Manthan Tilva Dec 30 '15 at 13:45
  • Thanks... But when I passed it `malformed HTTP request`..... It is not the way I think – Love_for_CODE Dec 30 '15 at 13:53
  • @ManthanTilva - Actually a TCP Connection Bytes contain TCP headers like IP version , Source, Destination...... My question is at which index is the http headers or the request is found in that byte array ?? – Love_for_CODE Dec 30 '15 at 13:55
  • 1
    @Love_for_CODE. As i say ,what's with golang i haven't idea. But in c/c++ when you use read system call on socket fd return by accept system call it will return payload of tcp layer in this case it is http header+content. I think same should be case with golang. – Manthan Tilva Dec 30 '15 at 13:59
  • When you read a TCP connection, you don't get TCP headers. I don't know what you think you see, but without an example we can only guess. – JimB Dec 30 '15 at 14:03
  • Further, you can potentially avoid this by using https://golang.org/pkg/net/http/httputil and its *Proxy types. – elithrar Dec 31 '15 at 10:31

1 Answers1

1

I am also new to golang but below code works for me as far as getting HTTP data from tcp socket is consern.

package main

import "net" 
import "fmt" 
import "io"

func main() {   
    fmt.Println("Launching server...")
    ln, _ := net.Listen("tcp", ":8081")
    conn, _ := ln.Accept()
    tmp := make([]byte,256)
        for {
            n,err := conn.Read(tmp)
            if err != nil {
                if err != io.EOF {
                    fmt.Println("read error:", err)
                }
                break
            }
            fmt.Println("rx:", string(tmp[:n]))
        } 
}
Manthan Tilva
  • 3,135
  • 2
  • 17
  • 41