add grpc api tls support

This commit is contained in:
Page Fault
2020-05-20 06:11:24 +00:00
parent e21d1101df
commit 7b636d3e52
8 changed files with 83 additions and 12 deletions
+13 -1
View File
@@ -2,6 +2,7 @@ package api
import (
"context"
"crypto/tls"
"net"
"github.com/p4gefau1t/trojan-go/common"
@@ -10,6 +11,7 @@ import (
"github.com/p4gefau1t/trojan-go/proxy"
"github.com/p4gefau1t/trojan-go/stat"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
type ClientAPI struct {
@@ -52,7 +54,17 @@ func (s *ClientAPI) GetTraffic(ctx context.Context, req *GetTrafficRequest) (*Ge
}
func RunClientAPI(ctx context.Context, config *conf.GlobalConfig, auth stat.Authenticator) error {
server := grpc.NewServer()
var server *grpc.Server
if config.API.APITLS {
creds := credentials.NewTLS(&tls.Config{
ClientAuth: tls.RequireAndVerifyClientCert,
Certificates: config.TLS.KeyPair,
ClientCAs: config.TLS.ClientCertPool,
})
server = grpc.NewServer(grpc.Creds(creds))
} else {
server = grpc.NewServer()
}
service := &ClientAPI{
ctx: ctx,
auth: auth,
+1
View File
@@ -7,6 +7,7 @@ import (
"github.com/p4gefau1t/trojan-go/common"
"github.com/p4gefau1t/trojan-go/conf"
_ "github.com/p4gefau1t/trojan-go/log/golog"
"github.com/p4gefau1t/trojan-go/stat/memory"
"google.golang.org/grpc"
)
+14 -1
View File
@@ -2,6 +2,7 @@ package api
import (
"context"
"crypto/tls"
"io"
"net"
@@ -11,6 +12,7 @@ import (
"github.com/p4gefau1t/trojan-go/proxy"
"github.com/p4gefau1t/trojan-go/stat"
grpc "google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
type ServerAPI struct {
@@ -152,7 +154,18 @@ func (s *ServerAPI) ListUsers(req *ListUserRequest, stream TrojanServerService_L
}
func RunServerAPI(ctx context.Context, config *conf.GlobalConfig, auth stat.Authenticator) error {
server := grpc.NewServer()
var server *grpc.Server
if config.API.APITLS {
creds := credentials.NewTLS(&tls.Config{
ClientAuth: tls.RequireAndVerifyClientCert,
Certificates: config.TLS.KeyPair,
ClientCAs: config.TLS.ClientCertPool,
})
server = grpc.NewServer(grpc.Creds(creds))
} else {
server = grpc.NewServer()
log.Warn("Using insecure API service. Please set \"api_tls\" to enable TLS-based gRPC service.")
}
service := &ServerAPI{
auth: auth,
}
+1
View File
@@ -8,6 +8,7 @@ import (
"github.com/p4gefau1t/trojan-go/common"
"github.com/p4gefau1t/trojan-go/conf"
_ "github.com/p4gefau1t/trojan-go/log/golog"
"github.com/p4gefau1t/trojan-go/stat/memory"
"google.golang.org/grpc"
)