Skip to content

Commit

Permalink
cli bugs fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
p4gefau1t committed Mar 27, 2020
1 parent bae1cfa commit 49e36d5
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 14 deletions.
23 changes: 21 additions & 2 deletions cert/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (

var logger = log.New(os.Stdout)
var caDir string = "https://acme-v02.api.letsencrypt.org/directory"
var tlsPort string = "443"
var httpPort string = "80"

type User struct {
Email string
Expand Down Expand Up @@ -118,12 +120,12 @@ func obtainCertificate(domain, email string, userKey *ecdsa.PrivateKey, serverKe
// (used later when we attempt to pass challenges). Keep in mind that you still
// need to proxy challenge traffic to port 5002 and 5001.
//err = client.Challenge.SetHTTP01Provider(http01.NewProviderServer("", "5002"))
err = client.Challenge.SetHTTP01Provider(http01.NewProviderServer("", ""))
err = client.Challenge.SetHTTP01Provider(http01.NewProviderServer("", httpPort))
if err != nil {
return nil, err
}
//err = client.Challenge.SetTLSALPN01Provider(tlsalpn01.NewProviderServer("", "5001"))
err = client.Challenge.SetTLSALPN01Provider(tlsalpn01.NewProviderServer("", ""))
err = client.Challenge.SetTLSALPN01Provider(tlsalpn01.NewProviderServer("", tlsPort))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -155,7 +157,24 @@ func obtainCertificate(domain, email string, userKey *ecdsa.PrivateKey, serverKe
return certificates, nil
}

func isFilesExist(nameList []string) bool {
fileInfo, err := ioutil.ReadDir("./")
common.Must(err)
for _, v := range fileInfo {
name := v.Name()
for _, u := range nameList {
if name == u {
return true
}
}
}
return false
}

func RequestCert(domain, email string) error {
if isFilesExist([]string{"server.key", "server.crt"}) {
return common.NewError("cert files(server.key, server.crt) already exist")
}
userKey, err := loadUserKey()
if err != nil {
logger.Warn("failed to load user key, trying to create one..")
Expand Down
8 changes: 4 additions & 4 deletions cert/cert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (

func TestCreate(t *testing.T) {
caDir = "https://127.0.0.1:14000/dir"
tlsPort = "5001"
httpPort = "5002"
common.Must(RequestCert("localhost", "[email protected]"))
}

func TestRenew(t *testing.T) {
caDir = "https://127.0.0.1:14000/dir"
tlsPort = "5001"
httpPort = "5002"
common.Must(RenewCert("localhost", "[email protected]"))
}

func TestCertGuide(t *testing.T) {
RequestCertGuide()
}
2 changes: 0 additions & 2 deletions cert/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ func askForConfirmation() bool {
}

func RequestCertGuide() {
//caDir = "https://127.0.0.1:14000/dir"
logger.Info("Guide mode: request cert")

logger.Warn("To perform a ACME challenge, trojan-go need the ROOT PRIVILEGE to bind port 80 and 443")
Expand Down Expand Up @@ -96,7 +95,6 @@ func RequestCertGuide() {
}

func RenewCertGuide() {
//caDir = "https://127.0.0.1:14000/dir"
logger.Info("Guide mode: renew cert")

logger.Warn("To perform a ACME challenge, trojan-go need the ROOT PRIVILEGE to bind port 80 and 443")
Expand Down
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ func main() {
case "renew":
cert.RenewCertGuide()
return
case "":
default:
logger.Error("Invalid cert arg")
return
}
data, err := ioutil.ReadFile(*configFile)
if err != nil {
Expand All @@ -44,6 +47,7 @@ func main() {

sigs := make(chan os.Signal, 1)
signal.Notify(sigs, os.Interrupt)
logger.Info("Trojan-Go interrupted")
select {
case <-sigs:
proxy.Close()
Expand Down
2 changes: 1 addition & 1 deletion proxy/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,11 @@ func (c *Client) Run() error {

func (c *Client) Close() error {
logger.Info("shutting down client..")
c.cancel()
c.muxClientLock.Lock()
defer c.muxClientLock.Unlock()
if c.muxClient != nil {
c.muxClient.Close()
}
c.cancel()
return nil
}
15 changes: 10 additions & 5 deletions proxy/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ import (
type Server struct {
common.Runnable

auth stat.Authenticator
meter stat.TrafficMeter
config *conf.GlobalConfig
ctx context.Context
cancel context.CancelFunc
listener net.Listener
auth stat.Authenticator
meter stat.TrafficMeter
config *conf.GlobalConfig
ctx context.Context
cancel context.CancelFunc
}

func (s *Server) handleMuxConn(stream *smux.Stream, passwordHash string) {
Expand Down Expand Up @@ -200,6 +201,7 @@ func (s *Server) Run() error {
return err
}
}
s.listener = listener
defer listener.Close()

tlsConfig := &tls.Config{
Expand Down Expand Up @@ -232,6 +234,9 @@ func (s *Server) Run() error {

func (s *Server) Close() error {
logger.Info("shutting down server..")
if s.listener != nil {
s.listener.Close()
}
s.cancel()
return nil
}

0 comments on commit 49e36d5

Please sign in to comment.