Skip to content

Commit

Permalink
$ brook hijackhttps
Browse files Browse the repository at this point in the history
  • Loading branch information
txthinking committed Jul 4, 2020
1 parent 38a785a commit ba3546c
Show file tree
Hide file tree
Showing 15 changed files with 13 additions and 1,735 deletions.
28 changes: 0 additions & 28 deletions OPENSOURCELICENSES
Original file line number Diff line number Diff line change
Expand Up @@ -167,34 +167,6 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

### gotun2socks

https://github.com/yinghuocho/gotun2socks

Copyright (c) 2016, yinghuocho
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

### minify

https://github.com/tdewolff/minify
Expand Down
127 changes: 6 additions & 121 deletions cli/brook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
_ "net/http/pprof"

"github.com/txthinking/brook"
"github.com/txthinking/brook/sysproxy"
"github.com/urfave/cli"
)

Expand Down Expand Up @@ -472,93 +471,10 @@ func main() {
},
&cli.Command{
Name: "tun",
Usage: "Run as tun, both TCP and UDP, [src <-> $ brook tun <-> $ brook server <-> dst], [works with $ brook server]",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "server",
Aliases: []string{"s"},
Usage: "Brook server address, like: 1.2.3.4:1080",
},
&cli.StringFlag{
Name: "password",
Aliases: []string{"p"},
Usage: "Brook server password",
},
&cli.StringFlag{
Name: "listen",
Aliases: []string{"l"},
Usage: "Listen address, MUST contain IP, like: 127.0.0.1:1080",
},
&cli.StringFlag{
Name: "dns",
Value: "8.8.8.8",
Usage: "DNS Server, like: 8.8.8.8",
},
&cli.IntFlag{
Name: "tcpTimeout",
Value: 60,
Usage: "connection tcp keepalive timeout (s)",
},
&cli.IntFlag{
Name: "tcpDeadline",
Value: 0,
Usage: "connection deadline time (s)",
},
&cli.IntFlag{
Name: "udpDeadline",
Value: 60,
Usage: "connection deadline time (s)",
},
&cli.IntFlag{
Name: "udpSessionTime",
Value: 60,
Usage: "udp session time (s), in most cases need this",
},
&cli.StringFlag{
Name: "tunDevice",
Usage: "tun name",
Value: "tun0",
},
&cli.StringFlag{
Name: "tunIP",
Usage: "tun IP",
Value: "10.9.9.2",
},
&cli.StringFlag{
Name: "tunGateway",
Usage: "tun gateway",
Value: "10.9.9.1",
},
&cli.StringFlag{
Name: "tunMask",
Usage: "tun mask",
Value: "255.255.255.0",
},
&cli.BoolFlag{
Name: "letBrookDoAllForMe",
Usage: "See more: https://github.com/txthinking/brook/wiki/How-to-run-tun-on-Linux,-macOS-and-Windows",
},
},
Usage: "tun",
Flags: []cli.Flag{},
Action: func(c *cli.Context) error {
if c.String("listen") == "" || c.String("server") == "" || c.String("password") == "" {
cli.ShowCommandHelp(c, "tun")
return nil
}
if debug {
enableDebug()
}
s, err := brook.NewTun(c.String("listen"), c.String("server"), c.String("password"), c.String("dns"), c.Int("tcpTimeout"), c.Int("tcpDeadline"), c.Int("udpDeadline"), c.Int("udpSessionTime"), c.String("tunDevice"), c.String("tunIP"), c.String("tunGateway"), c.String("tunMask"))
if err != nil {
return err
}
s.LetBrookDoAllForMe = c.Bool("letBrookDoAllForMe")
go func() {
log.Println(s.ListenAndServe())
}()
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
<-sigs
return s.Shutdown()
return errors.New("Deprecated, please try https://github.com/txthinking/ipio")
},
},
&cli.Command{
Expand Down Expand Up @@ -988,7 +904,7 @@ func main() {
Usage: "Socks5 password, optional",
},
&cli.StringFlag{
Name: "loopbackip",
Name: "listendnsip",
Usage: "127.0.0.1 or ::1, will create a DNS server with it, and listen TCP 443 on it",
Value: "127.0.0.1",
},
Expand Down Expand Up @@ -1026,7 +942,7 @@ func main() {
if debug {
enableDebug()
}
s, err := brook.NewHijackHTTPS(c.String("socks5"), c.String("socks5username"), c.String("socks5password"), c.String("loopbackip"), c.String("defaultDNSServer"), c.String("list"), c.Int("tcpTimeout"), c.Int("tcpDeadline"), c.Int("udpDeadline"))
s, err := brook.NewHijackHTTPS(c.String("socks5"), c.String("socks5username"), c.String("socks5password"), c.String("listendnsip"), c.String("defaultDNSServer"), c.String("list"), c.Int("tcpTimeout"), c.Int("tcpDeadline"), c.Int("udpDeadline"))
if err != nil {
return err
}
Expand Down Expand Up @@ -1096,38 +1012,6 @@ func main() {
return p.ListenAndServe()
},
},
&cli.Command{
Name: "systemproxy",
Usage: "Set system proxy with pac url, or remove, only works on macOS/Windows",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "url",
Aliases: []string{"u"},
Usage: "Pac address: like: http://127.0.0.1/pac",
},
&cli.BoolFlag{
Name: "remove",
Aliases: []string{"r"},
Usage: "Remove pac url from system proxy",
},
},
Action: func(c *cli.Context) error {
if !c.Bool("remove") && c.String("url") == "" {
cli.ShowCommandHelp(c, "systemproxy")
return nil
}
if c.Bool("remove") {
if err := sysproxy.TurnOffSystemProxy(); err != nil {
return err
}
return nil
}
if err := sysproxy.TurnOnSystemProxy(c.String("url")); err != nil {
return err
}
return nil
},
},
&cli.Command{
Name: "ssserver",
Usage: "Run as shadowsocks server, both TCP and UDP, fixed method is aes-256-cfb",
Expand Down Expand Up @@ -1331,6 +1215,7 @@ func main() {
fmt.Println("")
fmt.Println("Nami:", "https://github.com/txthinking/nami")
fmt.Println("Joker:", "https://github.com/txthinking/joker")
fmt.Println("Ipio:", "https://github.com/txthinking/ipio")
fmt.Println("")
return nil
},
Expand Down
24 changes: 7 additions & 17 deletions hijackhttps.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
// HijackHTTPS.
type HijackHTTPS struct {
Dial proxy.Dialer
LoopBackIP string
ListenDNSIP string
DefaultDNS string
Domains map[string]byte
UDPDNSServer *dns.Server
Expand All @@ -47,7 +47,7 @@ type HijackHTTPS struct {
}

// NewHijackHTTPS.
func NewHijackHTTPS(socks5addr, socks5username, socks5password, loopbackip, defaultDNS, list string, tcpTimeout, tcpDeadline, udpDeadline int) (*HijackHTTPS, error) {
func NewHijackHTTPS(socks5addr, socks5username, socks5password, listendnsip, defaultDNS, list string, tcpTimeout, tcpDeadline, udpDeadline int) (*HijackHTTPS, error) {
ds := make(map[string]byte)
if list != "" {
ss, err := readList(list)
Expand Down Expand Up @@ -85,7 +85,7 @@ func NewHijackHTTPS(socks5addr, socks5username, socks5password, loopbackip, defa
}
s := &HijackHTTPS{
Dial: dial,
LoopBackIP: loopbackip,
ListenDNSIP: listendnsip,
DefaultDNS: defaultDNS,
Domains: ds,
TCPTimeout: tcpTimeout,
Expand All @@ -98,13 +98,6 @@ func NewHijackHTTPS(socks5addr, socks5username, socks5password, loopbackip, defa

// Run server.
func (s *HijackHTTPS) ListenAndServe() error {
// l, err := sysproxy.GetDNSServers()
// if err != nil {
// return err
// }
// if err := sysproxy.SetDNSServers([]string{s.LoopBackIP}); err != nil {
// return err
// }
s.RunnerGroup.Add(&runnergroup.Runner{
Start: func() error {
return s.RunUDPDNSServer()
Expand Down Expand Up @@ -141,15 +134,12 @@ func (s *HijackHTTPS) ListenAndServe() error {
},
})
err := s.RunnerGroup.Wait()
// if err := sysproxy.SetDNSServers(l); err != nil {
// log.Println(err)
// }
return err
}

func (s *HijackHTTPS) RunUDPDNSServer() error {
s.UDPDNSServer = &dns.Server{
Addr: net.JoinHostPort(s.LoopBackIP, "53"),
Addr: net.JoinHostPort(s.ListenDNSIP, "53"),
Net: "udp",
ReadTimeout: time.Duration(s.UDPDeadline) * time.Second,
WriteTimeout: time.Duration(s.UDPDeadline) * time.Second,
Expand All @@ -160,7 +150,7 @@ func (s *HijackHTTPS) RunUDPDNSServer() error {

func (s *HijackHTTPS) RunTCPDNSServer() error {
s.TCPDNSServer = &dns.Server{
Addr: net.JoinHostPort(s.LoopBackIP, "53"),
Addr: net.JoinHostPort(s.ListenDNSIP, "53"),
Net: "tcp",
ReadTimeout: time.Duration(s.TCPTimeout) * time.Second,
WriteTimeout: time.Duration(s.TCPTimeout) * time.Second,
Expand Down Expand Up @@ -190,7 +180,7 @@ func (s *HijackHTTPS) DNSHandle(network string) dns.Handler {
m.Authoritative = true
m.Answer = append(m.Answer, &dns.A{
Hdr: dns.RR_Header{Name: m.Question[0].Name, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 60},
A: net.ParseIP(s.LoopBackIP),
A: net.ParseIP(s.ListenDNSIP),
})
w.WriteMsg(m)
return
Expand Down Expand Up @@ -229,7 +219,7 @@ func (s *HijackHTTPS) DNSHandle(network string) dns.Handler {

func (s *HijackHTTPS) RunHTTPSServer() error {
var err error
s.HTTPSServer, err = net.Listen("tcp", net.JoinHostPort(s.LoopBackIP, "443"))
s.HTTPSServer, err = net.Listen("tcp", net.JoinHostPort(s.ListenDNSIP, "443"))
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit ba3546c

Please sign in to comment.