2021-05-01 12:45:37 +02:00
package cmd
import (
"flag"
"fmt"
2022-03-26 12:08:54 +01:00
"github.com/netbirdio/netbird/encryption"
"github.com/netbirdio/netbird/signal/proto"
"github.com/netbirdio/netbird/signal/server"
"github.com/netbirdio/netbird/util"
2021-05-01 12:45:37 +02:00
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"google.golang.org/grpc"
2021-07-22 15:23:24 +02:00
"google.golang.org/grpc/credentials"
2021-07-21 20:23:11 +02:00
"google.golang.org/grpc/keepalive"
2021-05-01 12:45:37 +02:00
"net"
2021-08-15 16:56:26 +02:00
"net/http"
2021-07-21 20:23:11 +02:00
"os"
"time"
2021-05-01 12:45:37 +02:00
)
var (
2021-07-21 20:23:11 +02:00
signalPort int
signalLetsencryptDomain string
2021-08-13 08:46:30 +02:00
signalSSLDir string
2021-07-21 20:23:11 +02:00
signalKaep = grpc . KeepaliveEnforcementPolicy ( keepalive . EnforcementPolicy {
MinTime : 5 * time . Second ,
PermitWithoutStream : true ,
} )
signalKasp = grpc . KeepaliveParams ( keepalive . ServerParameters {
MaxConnectionIdle : 15 * time . Second ,
MaxConnectionAgeGrace : 5 * time . Second ,
Time : 5 * time . Second ,
Timeout : 2 * time . Second ,
} )
2021-05-01 12:45:37 +02:00
2021-08-13 08:46:30 +02:00
runCmd = & cobra . Command {
Use : "run" ,
Short : "start Wiretrustee Signal Server daemon" ,
2021-05-01 12:45:37 +02:00
Run : func ( cmd * cobra . Command , args [ ] string ) {
flag . Parse ( )
2021-09-07 09:53:18 +02:00
err := util . InitLog ( logLevel , logFile )
if err != nil {
log . Fatalf ( "failed initializing log %v" , err )
}
2021-05-01 12:45:37 +02:00
2021-07-21 20:23:11 +02:00
var opts [ ] grpc . ServerOption
2021-07-22 15:23:24 +02:00
if signalLetsencryptDomain != "" {
2021-08-13 08:46:30 +02:00
if _ , err := os . Stat ( signalSSLDir ) ; os . IsNotExist ( err ) {
err = os . MkdirAll ( signalSSLDir , os . ModeDir )
if err != nil {
log . Fatalf ( "failed creating datadir: %s: %v" , signalSSLDir , err )
}
}
certManager := encryption . CreateCertManager ( signalSSLDir , signalLetsencryptDomain )
2021-08-07 12:26:07 +02:00
transportCredentials := credentials . NewTLS ( certManager . TLSConfig ( ) )
2021-07-21 20:23:11 +02:00
opts = append ( opts , grpc . Creds ( transportCredentials ) )
2021-08-15 16:56:26 +02:00
listener := certManager . Listener ( )
log . Infof ( "http server listening on %s" , listener . Addr ( ) )
go func ( ) {
if err := http . Serve ( listener , certManager . HTTPHandler ( nil ) ) ; err != nil {
log . Errorf ( "failed to serve https server: %v" , err )
}
} ( )
2021-07-21 20:23:11 +02:00
}
opts = append ( opts , signalKaep , signalKasp )
grpcServer := grpc . NewServer ( opts ... )
2021-07-17 14:38:59 +02:00
lis , err := net . Listen ( "tcp" , fmt . Sprintf ( ":%d" , signalPort ) )
2021-05-01 12:45:37 +02:00
if err != nil {
log . Fatalf ( "failed to listen: %v" , err )
}
2021-08-13 08:46:30 +02:00
proto . RegisterSignalExchangeServer ( grpcServer , server . NewServer ( ) )
2021-07-17 14:38:59 +02:00
log . Printf ( "started server: localhost:%v" , signalPort )
2021-05-01 12:45:37 +02:00
if err := grpcServer . Serve ( lis ) ; err != nil {
log . Fatalf ( "failed to serve: %v" , err )
}
SetupCloseHandler ( )
2021-07-21 20:23:11 +02:00
<- stopCh
log . Println ( "Receive signal to stop running the Signal server" )
2021-05-01 12:45:37 +02:00
} ,
}
)
func init ( ) {
2021-08-13 08:46:30 +02:00
runCmd . PersistentFlags ( ) . IntVar ( & signalPort , "port" , 10000 , "Server port to listen on (e.g. 10000)" )
runCmd . Flags ( ) . StringVar ( & signalSSLDir , "ssl-dir" , "/var/lib/wiretrustee/" , "server ssl directory location. *Required only for Let's Encrypt certificates." )
runCmd . Flags ( ) . StringVar ( & signalLetsencryptDomain , "letsencrypt-domain" , "" , "a domain to issue Let's Encrypt certificate for. Enables TLS using Let's Encrypt. Will fetch and renew certificate, and run the server with TLS" )
2021-05-01 12:45:37 +02:00
}