11package main
22
33import (
4- "io "
5- "os "
4+ "context "
5+ "flag "
66 "fmt"
7+ "gopkg.in/yaml.v2"
8+ "io"
9+ "io/ioutil"
710 "net"
8- "flag"
9- "time"
10- "context"
11- "syscall"
12- "strings"
11+ "os"
1312 "os/signal"
14- "io/ioutil"
15- yaml "gopkg.in/yaml.v2"
13+ "strings"
14+ "syscall"
15+ "time"
1616)
1717
1818var SNIPort = 443
@@ -25,8 +25,8 @@ type conf struct {
2525
2626var (
2727 cfgfile = flag .String ("c" , "config.yaml" , "config file" )
28- FileLogPath = flag .String ("F " , "" , "log to file" )
29- EnableDebug = flag .Bool ("D " , false , "Enable debug" )
28+ FileLogPath = flag .String ("l " , "" , "log to file" )
29+ EnableDebug = flag .Bool ("d " , false , "Enable debug" )
3030)
3131
3232func main (){
@@ -40,17 +40,17 @@ func main(){
4040 serviceLogger (fmt .Sprintf ("Yaml file unmarshal failed: %v" , err ), 31 )
4141 os .Exit (0 )
4242 }
43- if ( len (cfg .ForwardRules ) <= 0 ) {
43+ if len (cfg .ForwardRules ) <= 0 {
4444 serviceLogger (fmt .Sprintf ("No rules found in yaml!" ), 31 )
4545 os .Exit (0 )
4646 }
4747 for _ , rule := range cfg .ForwardRules {
4848 serviceLogger (fmt .Sprintf ("Loaded rule: %v" , rule ), 32 )
4949 }
50- startSNIproxy ()
50+ startSniProxy ()
5151}
5252
53- func startSNIproxy (){
53+ func startSniProxy (){
5454 _ , cancel := context .WithCancel (context .Background ())
5555 defer cancel ()
5656 serviceLogger (fmt .Sprintf ("Starting SNI Proxy on port %v" , SNIPort ), 0 )
@@ -98,7 +98,7 @@ func serve(c net.Conn, raddr string) {
9898 }
9999
100100 for _ , rule := range cfg .ForwardRules {
101- if ( strings .Contains (servername , rule )) {
101+ if strings .Contains (servername , rule ) {
102102 serviceDebugger (fmt .Sprintf ("Found %v, forwarding to %s:%d" , servername , servername , ForwardPort ), 32 )
103103 forward (c , buf [:n ], fmt .Sprintf ("%s:%d" , servername , ForwardPort ), raddr )
104104 }
@@ -130,7 +130,7 @@ func getSNIServerName(buf []byte) string {
130130 //log.Printf("length: %d, got: %d", l, n)
131131
132132 // handshake message type
133- if uint8 ( buf [5 ]) != typeClientHello {
133+ if buf [5 ] != typeClientHello {
134134 serviceDebugger (fmt .Sprintf ("Not client hello" ), 31 )
135135 return ""
136136 }
@@ -162,29 +162,29 @@ func forward(conn net.Conn, data []byte, dst string, raddr string) {
162162 return
163163 }
164164
165- con_chk := make (chan int )
166- go ioReflector (backend , conn , false , con_chk , raddr , dst )
167- go ioReflector (conn , backend , true , con_chk , raddr , dst )
168- <- con_chk
165+ conChk := make (chan int )
166+ go ioReflector (backend , conn , false , conChk , raddr , dst )
167+ go ioReflector (conn , backend , true , conChk , raddr , dst )
168+ <- conChk
169169}
170170
171- func ioReflector (dst io.WriteCloser , src io.Reader , isToClient bool , con_chk chan int , raddr string , dsts string ) {
171+ func ioReflector (dst io.WriteCloser , src io.Reader , isToClient bool , conChk chan int , raddr string , dsts string ) {
172172 // Reflect IO stream to another.
173- defer on_disconnect (dst , con_chk )
173+ defer onDisconnect (dst , conChk )
174174 written , _ := io .Copy (dst , src )
175- if ( isToClient ) {
175+ if isToClient {
176176 serviceDebugger (fmt .Sprintf ("[%v] -> [%v], Written %d bytes" , dsts , raddr , written ), 33 )
177177 }else {
178178 serviceDebugger (fmt .Sprintf ("[%v] -> [%v], Written %d bytes" , raddr , dsts , written ), 33 )
179179 }
180180 dst .Close ()
181- con_chk <- 1
181+ conChk <- 1
182182}
183183
184- func on_disconnect (dst io.WriteCloser , con_chk chan int ){
184+ func onDisconnect (dst io.WriteCloser , conChk chan int ){
185185 // On Close-> Force Disconnect another pair of connection.
186- dst .Close ()
187- con_chk <- 1
186+ dst .Close ()
187+ conChk <- 1
188188}
189189
190190func (m * clientHelloMsg ) unmarshal (data []byte ) bool {
@@ -397,35 +397,35 @@ func (m *clientHelloMsg) unmarshal(data []byte) bool {
397397func serviceLogger (log string , color int ){
398398 log = strings .Replace (log , "\n " , "" , - 1 )
399399 log = strings .Join ([]string {time .Now ().Format ("2006/01/02 15:04:05" ), " " , log }, "" )
400- if ( color == 0 ) {
400+ if color == 0 {
401401 fmt .Printf ("%s\n " , log )
402402 }else {
403403 fmt .Printf ("%c[1;0;%dm%s%c[0m\n " , 0x1B , color , log , 0x1B )
404404 }
405- if ( * FileLogPath != "" ) {
405+ if * FileLogPath != "" {
406406 fd , _ := os .OpenFile (* FileLogPath , os .O_RDWR | os .O_CREATE | os .O_APPEND , 0644 )
407- fd_time := time .Now ().Format ("2006/01/02-15:04:05" );
408- fd_content := strings .Join ([]string {fd_time , " " , log , "\n " }, "" )
409- buf := []byte (fd_content )
410- fd .Write (buf )
407+ fdTime := time .Now ().Format ("2006/01/02-15:04:05" )
408+ fdContent := strings .Join ([]string {fdTime , " " , log , "\n " }, "" )
409+ buf := []byte (fdContent )
410+ fd .Write (buf )
411411 fd .Close ()
412412 }
413413}
414414
415415func serviceDebugger (log string , color int ){
416- if ( * EnableDebug ) {
416+ if * EnableDebug {
417417 log = strings .Replace (log , "\n " , "" , - 1 )
418418 log = strings .Join ([]string {time .Now ().Format ("2006/01/02 15:04:05" ), " [Debug] " , log }, "" )
419- if ( color == 0 ) {
419+ if color == 0 {
420420 fmt .Printf ("%s\n " , log )
421421 }else {
422422 fmt .Printf ("%c[1;0;%dm%s%c[0m\n " , 0x1B , color , log , 0x1B )
423423 }
424- if ( * FileLogPath != "" ) {
424+ if * FileLogPath != "" {
425425 fd , _ := os .OpenFile (* FileLogPath , os .O_RDWR | os .O_CREATE | os .O_APPEND , 0644 )
426- fd_time := time .Now ().Format ("2006/01/02-15:04:05" );
427- fd_content := strings .Join ([]string {fd_time , " " , log , "\n " }, "" )
428- buf := []byte (fd_content )
426+ fdTime := time .Now ().Format ("2006/01/02-15:04:05" )
427+ fdContent := strings .Join ([]string {fdTime , " " , log , "\n " }, "" )
428+ buf := []byte (fdContent )
429429 fd .Write (buf )
430430 fd .Close ()
431431 }
0 commit comments