2018-08-06 22:55:45 +00:00
package main
import (
"flag"
"fmt"
"os"
"strconv"
"strings"
2018-08-15 05:49:53 +00:00
2019-05-22 00:54:02 +00:00
sclient "git.rootprojects.org/root/sclient.go"
2018-08-06 22:55:45 +00:00
)
2020-12-02 22:04:58 +00:00
var (
// commit refers to the abbreviated commit hash
commit = "0000000"
// version refers to the most recent tag, plus any commits made since then
version = "v0.0.0-pre0+0000000"
// GitTimestamp refers to the timestamp of the most recent commit
date = "0000-00-00T00:00:00+0000"
)
func ver ( ) string {
return fmt . Sprintf ( "sclient %s (%s) %s" , version , commit [ : 7 ] , date )
}
2018-08-06 22:55:45 +00:00
func usage ( ) {
2021-06-22 05:22:34 +00:00
fmt . Fprintf ( os . Stderr , "\n%s\n" +
2020-12-02 22:04:58 +00:00
"\nusage: sclient <remote> <local>\n" +
2018-08-06 22:55:45 +00:00
"\n" +
" ex: sclient example.com 3000\n" +
" (sclient example.com:443 localhost:3000)\n" +
"\n" +
" ex: sclient example.com:8443 0.0.0.0:4080\n" +
2021-06-22 05:22:34 +00:00
"\n" +
" ex: sclient example.com:443 -\n" +
2020-12-02 22:04:58 +00:00
"\n" , ver ( ) )
2018-08-06 22:55:45 +00:00
flag . PrintDefaults ( )
fmt . Println ( )
}
func main ( ) {
2020-12-02 22:04:58 +00:00
if len ( os . Args ) >= 2 {
if "version" == strings . TrimLeft ( os . Args [ 1 ] , "-" ) {
fmt . Printf ( "%s\n" , ver ( ) )
os . Exit ( 0 )
return
}
}
2018-08-06 22:55:45 +00:00
flag . Usage = usage
2021-06-22 05:22:34 +00:00
insecure := flag . Bool ( "k" , false , "alias for --insecure" )
2021-06-22 05:05:27 +00:00
silent := flag . Bool ( "s" , false , "alias of --silent" )
2023-10-27 21:14:30 +00:00
servername := flag . String ( "servername" , "" , "specify a servername different from <remote> (to disable SNI use an IP as <remote> and do not use this option)" )
2018-08-06 22:55:45 +00:00
flag . BoolVar ( insecure , "insecure" , false , "ignore bad TLS/SSL/HTTPS certificates" )
2021-06-22 05:05:27 +00:00
flag . BoolVar ( silent , "silent" , false , "less verbose output" )
2018-08-06 22:55:45 +00:00
flag . Parse ( )
2018-08-08 06:16:52 +00:00
remotestr := flag . Arg ( 0 )
localstr := flag . Arg ( 1 )
2018-08-06 22:55:45 +00:00
i := flag . NArg ( )
if 2 != i {
2018-08-08 06:16:52 +00:00
// We may omit the second argument if we're going straight to stdin
if stat , _ := os . Stdin . Stat ( ) ; 1 == i && ( stat . Mode ( ) & os . ModeCharDevice ) == 0 {
localstr = "|"
} else {
usage ( )
os . Exit ( 1 )
}
2018-08-06 22:55:45 +00:00
}
2019-05-22 00:54:02 +00:00
sclient := & sclient . Tunnel {
2018-08-25 06:37:46 +00:00
RemotePort : 443 ,
LocalAddress : "localhost" ,
InsecureSkipVerify : * insecure ,
ServerName : * servername ,
2021-06-22 05:05:27 +00:00
Silent : * silent ,
2018-08-25 06:37:46 +00:00
}
2018-08-06 22:55:45 +00:00
2018-08-08 06:16:52 +00:00
remote := strings . Split ( remotestr , ":" )
//remoteAddr, remotePort, err := net.SplitHostPort(remotestr)
2018-08-06 22:55:45 +00:00
if 2 == len ( remote ) {
rport , err := strconv . Atoi ( remote [ 1 ] )
if nil != err {
usage ( )
os . Exit ( 0 )
}
2019-05-22 00:54:02 +00:00
sclient . RemotePort = rport
2018-08-06 22:55:45 +00:00
} else if 1 != len ( remote ) {
usage ( )
os . Exit ( 0 )
}
2019-05-22 00:54:02 +00:00
sclient . RemoteAddress = remote [ 0 ]
2018-08-06 22:55:45 +00:00
2018-08-08 06:16:52 +00:00
if "-" == localstr || "|" == localstr {
// User may specify stdin/stdout instead of net
2019-05-22 00:54:02 +00:00
sclient . LocalAddress = localstr
sclient . LocalPort = - 1
2018-08-06 22:55:45 +00:00
} else {
2018-08-08 06:16:52 +00:00
// Test that argument is a local address
local := strings . Split ( localstr , ":" )
if 1 == len ( local ) {
lport , err := strconv . Atoi ( local [ 0 ] )
if nil != err {
usage ( )
os . Exit ( 0 )
}
2019-05-22 00:54:02 +00:00
sclient . LocalPort = lport
2018-08-08 06:16:52 +00:00
} else {
lport , err := strconv . Atoi ( local [ 1 ] )
if nil != err {
usage ( )
os . Exit ( 0 )
}
2019-05-22 00:54:02 +00:00
sclient . LocalAddress = local [ 0 ]
sclient . LocalPort = lport
2018-08-06 22:55:45 +00:00
}
}
2019-05-22 00:54:02 +00:00
err := sclient . DialAndListen ( )
2018-08-06 22:55:45 +00:00
if nil != err {
2018-08-08 06:16:52 +00:00
fmt . Fprintf ( os . Stderr , "%s\n" , err )
//usage()
//os.Exit(6)
2018-08-06 22:55:45 +00:00
}
}