Skip to content

Instantly share code, notes, and snippets.

@tcortega
Created March 5, 2024 06:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tcortega/c1086c1fc6798e9690bf163e80c6f64c to your computer and use it in GitHub Desktop.
Save tcortega/c1086c1fc6798e9690bf163e80c6f64c to your computer and use it in GitHub Desktop.
manually create conn with an http proxy in go
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