forked from txthinking/brook
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a732259
commit 32026ed
Showing
6 changed files
with
138 additions
and
120 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
main | ||
brook | ||
brook386 | ||
brookmacos | ||
brookserver.exe | ||
brookserver.386.exe | ||
brook_linux_386 | ||
brook_linux_arm64 | ||
brook_linux_arm_7 | ||
brook_linux_arm_6 | ||
brook_linux_arm_5 | ||
brook_macos_amd64 | ||
brook_windows_amd64.exe | ||
brook_windows_386.exe |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,11 @@ | ||
#!/bin/bash | ||
|
||
GOOS=linux GOARCH=amd64 go build -o brook . | ||
GOOS=linux GOARCH=386 go build -o brook386 . | ||
GOOS=darwin GOARCH=amd64 go build -o brookmacos . | ||
GOOS=windows GOARCH=amd64 go build -o brookserver.exe . | ||
GOOS=windows GOARCH=386 go build -o brookserver.386.exe . | ||
GOOS=linux GOARCH=386 go build -o brook_linux_386 . | ||
GOOS=linux GOARCH=arm64 go build -o brook_linux_arm64 . | ||
GOOS=linux GOARCH=arm GOARM=7 go build -o brook_linux_arm_7 . | ||
GOOS=linux GOARCH=arm GOARM=6 go build -o brook_linux_arm_6 . | ||
GOOS=linux GOARCH=arm GOARM=5 go build -o brook_linux_arm_5 . | ||
GOOS=darwin GOARCH=amd64 go build -o brook_macos_amd64 . | ||
GOOS=windows GOARCH=amd64 go build -o brook_windows_amd64.exe . | ||
GOOS=windows GOARCH=386 go build -o brook_windows_386.exe . |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package brook | ||
|
||
import ( | ||
"io" | ||
"net" | ||
"time" | ||
|
||
"github.com/txthinking/pac/blackwhite" | ||
) | ||
|
||
// WhiteHTTPMiddleman is a HTTPMiddleman who only handle domain in white list | ||
type WhiteHTTPMiddleman struct { | ||
Timeout int | ||
Deadline int | ||
} | ||
|
||
// NewWhiteHTTPMiddleman returns a WhiteHTTPMiddleman which can used to handle http proxy request | ||
func NewWhiteHTTPMiddleman(timeout, deadline int) *WhiteHTTPMiddleman { | ||
blackwhite.InitWhiteList() | ||
return &WhiteHTTPMiddleman{ | ||
Timeout: timeout, | ||
Deadline: deadline, | ||
} | ||
} | ||
|
||
// HandleHTTPProxy handle http proxy request, if the domain is in the white list | ||
func (w *WhiteHTTPMiddleman) HandleHTTPProxy(method, addr string, request []byte, conn *net.TCPConn) (handled bool, err error) { | ||
host, _, err := net.SplitHostPort(addr) | ||
if err != nil { | ||
return false, err | ||
} | ||
if !blackwhite.IsWhite(host) { | ||
return false, nil | ||
} | ||
|
||
rc, err := net.Dial("tcp", addr) | ||
if err != nil { | ||
return true, err | ||
} | ||
defer rc.Close() | ||
if w.Timeout != 0 { | ||
if rtc, ok := rc.(*net.TCPConn); ok { | ||
if err := rtc.SetKeepAlivePeriod(time.Duration(w.Timeout) * time.Second); err != nil { | ||
return true, err | ||
} | ||
} | ||
} | ||
if w.Deadline != 0 { | ||
if err := rc.SetDeadline(time.Now().Add(time.Duration(w.Deadline) * time.Second)); err != nil { | ||
return true, err | ||
} | ||
} | ||
if method == "CONNECT" { | ||
_, err := conn.Write([]byte("HTTP/1.1 200 Connection established\r\n\r\n")) | ||
if err != nil { | ||
return true, err | ||
} | ||
} | ||
if method != "CONNECT" { | ||
if _, err := rc.Write(request); err != nil { | ||
return true, err | ||
} | ||
} | ||
go func() { | ||
_, _ = io.Copy(rc, conn) | ||
}() | ||
_, _ = io.Copy(conn, rc) | ||
return true, nil | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package brook | ||
|
||
import ( | ||
"io" | ||
"net" | ||
|
||
"github.com/txthinking/pac/blackwhite" | ||
"github.com/txthinking/socks5" | ||
) | ||
|
||
// WhiteSocks5Middleman is a Socks5Middleman who only handle domain in white list | ||
type WhiteSocks5Middleman struct { | ||
Timeout int | ||
Deadline int | ||
Dial Dialer | ||
} | ||
|
||
// NewWhiteSocks5Middleman returns a WhiteSocks5Middleman which can used to handle http proxy request | ||
func NewWhiteSocks5Middleman(timeout, deadline int, dial Dialer) *WhiteSocks5Middleman { | ||
blackwhite.InitWhiteList() | ||
if dial == nil { | ||
dial = &DefaultDial{} | ||
} | ||
return &WhiteSocks5Middleman{ | ||
Timeout: timeout, | ||
Deadline: deadline, | ||
Dial: dial, | ||
} | ||
} | ||
|
||
// HandleSocks5Proxy handle http proxy request, if the domain is in the white list | ||
func (w *WhiteSocks5Middleman) HandleSocks5Proxy(request *socks5.Request, c *net.TCPConn) (handled bool, err error) { | ||
addr := request.Address() | ||
host, _, err := net.SplitHostPort(addr) | ||
if err != nil { | ||
return false, err | ||
} | ||
if !blackwhite.IsWhite(host) { | ||
return false, nil | ||
} | ||
|
||
rc, err := request.ConnectWithDial(c, w.Dial) | ||
if err != nil { | ||
return false, err | ||
} | ||
defer rc.Close() | ||
go func() { | ||
_, _ = io.Copy(rc, c) | ||
}() | ||
_, _ = io.Copy(c, rc) | ||
return true, nil | ||
} |