Skip to content

Commit

Permalink
build for ARM
Browse files Browse the repository at this point in the history
  • Loading branch information
txthinking committed Jul 16, 2017
1 parent a732259 commit 32026ed
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 120 deletions.
12 changes: 8 additions & 4 deletions cli/brook/.gitignore
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
12 changes: 8 additions & 4 deletions cli/brook/buildAll.sh
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 .
68 changes: 1 addition & 67 deletions httpmiddleman.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
package brook

import (
"io"
"net"
"time"

"github.com/txthinking/pac/blackwhite"
)
import "net"

// HTTPMiddleman is a middleman who can intercept and handle request
type HTTPMiddleman interface {
Expand All @@ -15,63 +9,3 @@ type HTTPMiddleman interface {
// if handled is true or err is not nil that means the request has been handled
HandleHTTPProxy(method, addr string, request []byte, conn *net.TCPConn) (handled bool, err error)
}

// 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
}
45 changes: 0 additions & 45 deletions socks5middleman.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package brook

import (
"io"
"net"

"github.com/txthinking/pac/blackwhite"
"github.com/txthinking/socks5"
)

Expand All @@ -14,46 +12,3 @@ type Socks5Middleman interface {
// if handled is true or err is not nil that means the request has been handled
HandleSocks5Proxy(request *socks5.Request, conn *net.TCPConn) (handled bool, err error)
}

// 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
}
69 changes: 69 additions & 0 deletions whitehttpmiddleman.go
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
}
52 changes: 52 additions & 0 deletions whitesocks5middleman.go
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
}

0 comments on commit 32026ed

Please sign in to comment.