mirror of
https://github.com/p4gefau1t/trojan-go.git
synced 2024-04-21 12:21:34 +00:00
add grpc api tls support
This commit is contained in:
+13
-1
@@ -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,
|
||||
|
||||
@@ -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
@@ -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,
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user