Skip to content

Commit

Permalink
Merge branch 'refactor/codes'
Browse files Browse the repository at this point in the history
  • Loading branch information
wweir committed Mar 10, 2019
2 parents 89adc6a + 26055c5 commit 5b217b7
Show file tree
Hide file tree
Showing 33 changed files with 371 additions and 435 deletions.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
name: Prepare Environment
command: |
cp conf/sower.toml .
cp deploy/* .
cp .circleci/* .
mkdir artifacts
- run:
name: Run Unit Test
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ build:
"-X main.version=$(shell git describe --tags) \
-X main.date=$(shell date +%Y-%m-%d)"
image:
docker build -t sower -f deploy/Dockerfile .
docker build -t sower -f .circleci/Dockerfile .

kill:
sudo pkill -9 sower || true
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ After Deployed, please check your config file, it is placed in `/usr/local/etc/s
Auto deploy script support Linux server side and masOS/Linux client side.

```shell
$ bash -c "$(curl -s https://raw.githubusercontent.com/wweir/sower/master/deploy/install)"
$ bash -c "$(curl -s https://raw.githubusercontent.com/wweir/sower/master/.circleci/install)"
```

Then modify the configuration file as needed and set `127.0.0.1` as your first domain name server.
Expand Down
5 changes: 4 additions & 1 deletion conf/conf_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ import (
"flag"
"os"
"path/filepath"
"strings"

"github.com/wweir/sower/proxy/transport"
)

func initArgs() {
flag.StringVar(&Conf.ConfigFile, "f", filepath.Dir(os.Args[0])+"/sower.toml", "config file location")
flag.StringVar(&Conf.NetType, "n", "TCP", "proxy net type (QUIC|KCP|TCP)")
flag.StringVar(&Conf.NetType, "n", "TCP", "proxy net type ("+strings.Join(transport.ListTransports(), "|")+")")
flag.StringVar(&Conf.Cipher, "C", "AES_128_GCM", "cipher type (AES_128_GCM|AES_192_GCM|AES_256_GCM|CHACHA20_IETF_POLY1305|XCHACHA20_IETF_POLY1305)")
flag.StringVar(&Conf.Password, "p", "12345678", "password")
flag.StringVar(&Conf.ServerPort, "P", "5533", "server mode listen port")
Expand Down
12 changes: 9 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/wweir/sower/conf"
"github.com/wweir/sower/dns"
"github.com/wweir/sower/proxy"
"github.com/wweir/sower/proxy/transport"
)

var version, date string
Expand All @@ -13,16 +14,21 @@ func main() {
conf := conf.Conf
glog.Infof("Starting sower(%s %s): %v", version, date, conf)

tran, err := transport.GetTransport(conf.NetType)
if err != nil {
glog.Exitln(err)
}

if conf.ServerAddr == "" {
proxy.StartServer(conf.NetType, conf.ServerPort, conf.Cipher, conf.Password)
proxy.StartServer(tran, conf.ServerPort, conf.Cipher, conf.Password)

} else {
if conf.HTTPProxy != "" {
go proxy.StartHttpProxy(conf.NetType, conf.ServerAddr,
go proxy.StartHttpProxy(tran, conf.ServerAddr,
conf.Cipher, conf.Password, conf.HTTPProxy)
}

go dns.StartDNS(conf.DNSServer, conf.ClientIP)
proxy.StartClient(conf.NetType, conf.ServerAddr, conf.Cipher, conf.Password, conf.ClientIP)
proxy.StartClient(tran, conf.ServerAddr, conf.Cipher, conf.Password, conf.ClientIP)
}
}
29 changes: 4 additions & 25 deletions proxy/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,12 @@ import (
"net"

"github.com/golang/glog"
"github.com/wweir/sower/proxy/kcp"
"github.com/wweir/sower/proxy/quic"
"github.com/wweir/sower/proxy/tcp"
"github.com/wweir/sower/shadow"
"github.com/wweir/sower/proxy/shadow"
"github.com/wweir/sower/proxy/transport"
)

type Client interface {
Dial(server string) (net.Conn, error)
}

func NewClient(netType string) Client {
switch netType {
case QUIC.String():
return quic.NewClient()
case KCP.String():
return kcp.NewClient()
case TCP.String():
return tcp.NewClient()
default:
glog.Fatalln("invalid net type: " + netType)
return nil
}
}

func StartClient(netType, server, cipher, password, listenIP string) {
func StartClient(tran transport.Transport, server, cipher, password, listenIP string) {
connCh := listenLocal(listenIP, []string{"80", "443"})
client := NewClient(netType)
resolved := false

glog.Infoln("Client started.")
Expand All @@ -47,7 +26,7 @@ func StartClient(netType, server, cipher, password, listenIP string) {
}
glog.V(1).Infof("new conn from (%s) to (%s)", conn.RemoteAddr(), server)

rc, err := client.Dial(server)
rc, err := tran.Dial(server)
if err != nil {
conn.Close()
glog.Errorln(err)
Expand Down
22 changes: 13 additions & 9 deletions proxy/http_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
"time"

"github.com/golang/glog"
"github.com/wweir/sower/shadow"
"github.com/wweir/sower/proxy/shadow"
"github.com/wweir/sower/proxy/transport"
)

func StartHttpProxy(netType, server, cipher, password, addr string) {
client := NewClient(netType)
func StartHttpProxy(tran transport.Transport, server, cipher, password, addr string) {
resolved := false

srv := &http.Server{
Expand All @@ -29,9 +29,9 @@ func StartHttpProxy(netType, server, cipher, password, addr string) {
}

if r.Method == http.MethodConnect {
httpsProxy(w, r, client, server, cipher, password)
httpsProxy(w, r, tran, server, cipher, password)
} else {
httpProxy(w, r, client, server, cipher, password)
httpProxy(w, r, tran, server, cipher, password)
}
}),
// Disable HTTP/2.
Expand All @@ -42,10 +42,12 @@ func StartHttpProxy(netType, server, cipher, password, addr string) {
glog.Fatalln(srv.ListenAndServe())
}

func httpProxy(w http.ResponseWriter, req *http.Request, client Client, server, cipher, password string) {
func httpProxy(w http.ResponseWriter, req *http.Request,
tran transport.Transport, server, cipher, password string) {

roundTripper := &http.Transport{
DialContext: func(context.Context, string, string) (net.Conn, error) {
conn, err := client.Dial(server)
conn, err := tran.Dial(server)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -74,7 +76,9 @@ func httpProxy(w http.ResponseWriter, req *http.Request, client Client, server,
io.Copy(w, resp.Body)
}

func httpsProxy(w http.ResponseWriter, r *http.Request, client Client, server, cipher, password string) {
func httpsProxy(w http.ResponseWriter, r *http.Request,
tran transport.Transport, server, cipher, password string) {

// local conn
conn, _, err := w.(http.Hijacker).Hijack()
if err != nil {
Expand All @@ -90,7 +94,7 @@ func httpsProxy(w http.ResponseWriter, r *http.Request, client Client, server, c
}

// remote conn
rc, err := client.Dial(server)
rc, err := tran.Dial(server)
if err != nil {
conn.Close()
http.Error(w, err.Error(), http.StatusServiceUnavailable)
Expand Down
65 changes: 0 additions & 65 deletions proxy/kcp/client.go

This file was deleted.

56 changes: 0 additions & 56 deletions proxy/kcp/server.go

This file was deleted.

16 changes: 0 additions & 16 deletions proxy/nettype_string.go

This file was deleted.

2 changes: 1 addition & 1 deletion parse/parse_addr.go → proxy/parser/parse_addr.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package parse
package parser

import (
"bufio"
Expand Down
2 changes: 1 addition & 1 deletion parse/sni.go → proxy/parser/sni.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package parse
package parser

import (
"encoding/binary"
Expand Down
56 changes: 0 additions & 56 deletions proxy/quic/client.go

This file was deleted.

Loading

0 comments on commit 5b217b7

Please sign in to comment.