http: add basic auth in client mode

This commit is contained in:
nadoo 2017-12-17 00:36:53 +08:00
parent 7a9609771f
commit 632f45448b
5 changed files with 17 additions and 6 deletions

View File

@ -35,7 +35,7 @@ DNS Forwarding Server(udp2tcp):
- Add resolved IPs to proxy rules - Add resolved IPs to proxy rules
- Add resolved IPs to ipset - Add resolved IPs to ipset
Ipset Management: IPSet Management:
- Add ip/cidrs from rule files on startup - Add ip/cidrs from rule files on startup
- Add resolved ips for domains from rule files by dns forwarding server - Add resolved ips for domains from rule files by dns forwarding server
@ -47,7 +47,7 @@ General:
- Rule proxy based on destinations: [Config Examples](config/examples) - Rule proxy based on destinations: [Config Examples](config/examples)
TODO: TODO:
- [x] UDP over TCP Tunnel (client <-udp-> uottun <-tcp-> ss <-udp-> target) - [x] UDP over TCP Tunnel (client <--udp--> glider/uottun <--tcp--> ss <--udp--> target)
- [ ] Transparent UDP proxy (linux tproxy) - [ ] Transparent UDP proxy (linux tproxy)
- [ ] TUN/TAP device support - [ ] TUN/TAP device support
- [ ] Code refactoring: support proxy registering so it can be pluggable - [ ] Code refactoring: support proxy registering so it can be pluggable

View File

@ -40,7 +40,7 @@ func DialerFromURL(s string, cDialer Dialer) (Dialer, error) {
switch u.Scheme { switch u.Scheme {
case "http": case "http":
return NewHTTP(addr, cDialer, nil) return NewHTTP(addr, user, pass, cDialer, nil)
case "socks5": case "socks5":
return NewSOCKS5(addr, user, pass, cDialer, nil) return NewSOCKS5(addr, user, pass, cDialer, nil)
case "ss": case "ss":

13
http.go
View File

@ -6,6 +6,7 @@ package main
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"encoding/base64"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@ -20,13 +21,18 @@ import (
type HTTP struct { type HTTP struct {
*Forwarder // as client *Forwarder // as client
sDialer Dialer // dialer for server sDialer Dialer // dialer for server
user string
password string
} }
// NewHTTP returns a http proxy. // NewHTTP returns a http proxy.
func NewHTTP(addr string, cDialer Dialer, sDialer Dialer) (*HTTP, error) { func NewHTTP(addr, user, pass string, cDialer Dialer, sDialer Dialer) (*HTTP, error) {
s := &HTTP{ s := &HTTP{
Forwarder: NewForwarder(addr, cDialer), Forwarder: NewForwarder(addr, cDialer),
sDialer: sDialer, sDialer: sDialer,
user: user,
password: pass,
} }
return s, nil return s, nil
@ -186,6 +192,11 @@ func (s *HTTP) Dial(network, addr string) (net.Conn, error) {
rc.Write([]byte("CONNECT " + addr + " HTTP/1.0\r\n")) rc.Write([]byte("CONNECT " + addr + " HTTP/1.0\r\n"))
// c.Write([]byte("Proxy-Connection: Keep-Alive\r\n")) // c.Write([]byte("Proxy-Connection: Keep-Alive\r\n"))
if s.user != "" && s.password != "" {
auth := s.user + ":" + s.password
rc.Write([]byte("Authorization: Basic " + base64.StdEncoding.EncodeToString([]byte(auth)) + "\r\n"))
}
var b [1024]byte var b [1024]byte
n, err := rc.Read(b[:]) n, err := rc.Read(b[:])
if bytes.Contains(b[:n], []byte("200")) { if bytes.Contains(b[:n], []byte("200")) {

View File

@ -33,7 +33,7 @@ func NewMixedProxy(addr, user, pass string, sDialer Dialer) (*MixedProxy, error)
addr: addr, addr: addr,
} }
p.http, _ = NewHTTP(addr, nil, sDialer) p.http, _ = NewHTTP(addr, user, pass, nil, sDialer)
p.socks5, _ = NewSOCKS5(addr, user, pass, nil, sDialer) p.socks5, _ = NewSOCKS5(addr, user, pass, nil, sDialer)
return p, nil return p, nil

View File

@ -40,7 +40,7 @@ func ServerFromURL(s string, sDialer Dialer) (Server, error) {
case "mixed": case "mixed":
return NewMixedProxy(addr, user, pass, sDialer) return NewMixedProxy(addr, user, pass, sDialer)
case "http": case "http":
return NewHTTP(addr, nil, sDialer) return NewHTTP(addr, user, pass, nil, sDialer)
case "socks5": case "socks5":
return NewSOCKS5(addr, user, pass, nil, sDialer) return NewSOCKS5(addr, user, pass, nil, sDialer)
case "ss": case "ss":