Created
March 5, 2024 06:42
-
-
Save tcortega/c1086c1fc6798e9690bf163e80c6f64c to your computer and use it in GitHub Desktop.
manually create conn with an http proxy in go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func (p *Proxy) GetHttpImapConnection(imapServer string) (net.Conn, error) { | |
proxyAddr := p.IpAddress + ":" + strconv.Itoa(p.Port) | |
conn, err := net.Dial("tcp", proxyAddr) | |
if err != nil { | |
return nil, err | |
} | |
request := "CONNECT " + imapServer + " HTTP/1.1\r\nHost: " + imapServer + "\r\n" | |
if p.HasCredentials() { | |
credentials := p.GetEncodedCredentials() | |
request += "Proxy-Authorization: Basic " + credentials + "\r\n\r\n" | |
} | |
_, err = conn.Write([]byte(request)) | |
if err != nil { | |
conn.Close() | |
return nil, err | |
} | |
response, err := bufio.NewReader(conn).ReadString('\n') | |
if err != nil { | |
conn.Close() | |
return nil, err | |
} | |
if response[:12] != "HTTP/1.1 200" { | |
conn.Close() | |
return nil, errors.New("proxy connection failed: " + response) | |
} | |
conn = tls.Client(conn, &tls.Config{InsecureSkipVerify: true}) | |
return conn, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment