| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- /*
- * Copyright (c) 2016, Psiphon Inc.
- * All rights reserved.
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- */
- // Package psiphon/server implements the core tunnel functionality of a Psiphon server.
- // The main function is RunServices, which runs one or all of a Psiphon API web server,
- // a tunneling SSH server, and an Obfuscated SSH protocol server. The server configuration
- // is created by the GenerateConfig function.
- package server
- import (
- "os"
- "os/signal"
- "runtime"
- "sync"
- "syscall"
- "time"
- "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon"
- "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/server/psinet"
- )
- // RunServices initializes support functions including logging and GeoIP services;
- // and then starts the server components and runs them until os.Interrupt or
- // os.Kill signals are received. The config determines which components are run.
- func RunServices(configJSON []byte) error {
- config, err := LoadConfig(configJSON)
- if err != nil {
- log.WithContextFields(LogFields{"error": err}).Error("load config failed")
- return psiphon.ContextError(err)
- }
- err = InitLogging(config)
- if err != nil {
- log.WithContextFields(LogFields{"error": err}).Error("init logging failed")
- return psiphon.ContextError(err)
- }
- supportServices, err := NewSupportServices(config)
- if err != nil {
- log.WithContextFields(LogFields{"error": err}).Error("init support services failed")
- return psiphon.ContextError(err)
- }
- waitGroup := new(sync.WaitGroup)
- shutdownBroadcast := make(chan struct{})
- errors := make(chan error)
- tunnelServer, err := NewTunnelServer(supportServices, shutdownBroadcast)
- if err != nil {
- log.WithContextFields(LogFields{"error": err}).Error("init tunnel server failed")
- return psiphon.ContextError(err)
- }
- if config.RunLoadMonitor() {
- waitGroup.Add(1)
- go func() {
- waitGroup.Done()
- ticker := time.NewTicker(time.Duration(config.LoadMonitorPeriodSeconds) * time.Second)
- defer ticker.Stop()
- for {
- select {
- case <-shutdownBroadcast:
- return
- case <-ticker.C:
- logServerLoad(tunnelServer)
- }
- }
- }()
- }
- if config.RunWebServer() {
- waitGroup.Add(1)
- go func() {
- defer waitGroup.Done()
- err := RunWebServer(supportServices, shutdownBroadcast)
- select {
- case errors <- err:
- default:
- }
- }()
- }
- // The tunnel server is always run; it launches multiple
- // listeners, depending on which tunnel protocols are enabled.
- waitGroup.Add(1)
- go func() {
- defer waitGroup.Done()
- err := tunnelServer.Run()
- select {
- case errors <- err:
- default:
- }
- }()
- // An OS signal triggers an orderly shutdown
- systemStopSignal := make(chan os.Signal, 1)
- signal.Notify(systemStopSignal, os.Interrupt, os.Kill)
- // SIGUSR1 triggers a reload of support services
- reloadSupportServicesSignal := make(chan os.Signal, 1)
- signal.Notify(reloadSupportServicesSignal, syscall.SIGUSR1)
- // SIGUSR2 triggers an immediate load log
- logServerLoadSignal := make(chan os.Signal, 1)
- signal.Notify(logServerLoadSignal, syscall.SIGUSR2)
- err = nil
- loop:
- for {
- select {
- case <-reloadSupportServicesSignal:
- supportServices.Reload()
- case <-logServerLoadSignal:
- logServerLoad(tunnelServer)
- case <-systemStopSignal:
- log.WithContext().Info("shutdown by system")
- break loop
- case err = <-errors:
- log.WithContextFields(LogFields{"error": err}).Error("service failed")
- break loop
- }
- }
- close(shutdownBroadcast)
- waitGroup.Wait()
- return err
- }
- func logServerLoad(server *TunnelServer) {
- // golang runtime stats
- var memStats runtime.MemStats
- runtime.ReadMemStats(&memStats)
- fields := LogFields{
- "NumGoroutine": runtime.NumGoroutine(),
- "MemStats.Alloc": memStats.Alloc,
- "MemStats.TotalAlloc": memStats.TotalAlloc,
- "MemStats.Sys": memStats.Sys,
- "MemStats.PauseTotalNs": memStats.PauseTotalNs,
- "MemStats.PauseNs": memStats.PauseNs,
- "MemStats.NumGC": memStats.NumGC,
- "MemStats.GCCPUFraction": memStats.GCCPUFraction,
- }
- // tunnel server stats
- for tunnelProtocol, stats := range server.GetLoadStats() {
- for stat, value := range stats {
- fields[tunnelProtocol+"."+stat] = value
- }
- }
- log.WithContextFields(fields).Info("load")
- }
- // SupportServices carries common and shared data components
- // across different server components. SupportServices implements a
- // hot reload of traffic rules, psinet database, and geo IP database
- // components, which allows these data components to be refreshed
- // without restarting the server process.
- type SupportServices struct {
- Config *Config
- TrafficRulesSet *TrafficRulesSet
- PsinetDatabase *psinet.Database
- GeoIPService *GeoIPService
- DNSResolver *DNSResolver
- }
- // NewSupportServices initializes a new SupportServices.
- func NewSupportServices(config *Config) (*SupportServices, error) {
- trafficRulesSet, err := NewTrafficRulesSet(config.TrafficRulesFilename)
- if err != nil {
- return nil, psiphon.ContextError(err)
- }
- psinetDatabase, err := psinet.NewDatabase(config.PsinetDatabaseFilename)
- if err != nil {
- return nil, psiphon.ContextError(err)
- }
- geoIPService, err := NewGeoIPService(
- config.GeoIPDatabaseFilenames, config.DiscoveryValueHMACKey)
- if err != nil {
- return nil, psiphon.ContextError(err)
- }
- dnsResolver, err := NewDNSResolver(config.DNSResolverIPAddress)
- if err != nil {
- return nil, psiphon.ContextError(err)
- }
- return &SupportServices{
- Config: config,
- TrafficRulesSet: trafficRulesSet,
- PsinetDatabase: psinetDatabase,
- GeoIPService: geoIPService,
- DNSResolver: dnsResolver,
- }, nil
- }
- // Reload reinitializes traffic rules, psinet database, and geo IP database
- // components. If any component fails to reload, an error is logged and
- // Reload proceeds, using the previous state of the component.
- //
- // Limitation: reload of traffic rules currently doesn't apply to existing,
- // established clients.
- func (support *SupportServices) Reload() {
- reloaders := append(
- []psiphon.Reloader{support.TrafficRulesSet, support.PsinetDatabase},
- support.GeoIPService.Reloaders()...)
- for _, reloader := range reloaders {
- if !reloader.WillReload() {
- // Skip logging
- continue
- }
- // "reloaded" flag indicates if file was actually reloaded or ignored
- reloaded, err := reloader.Reload()
- if err != nil {
- log.WithContextFields(
- LogFields{
- "reloader": reloader.LogDescription(),
- "error": err}).Error("reload failed")
- // Keep running with previous state
- } else {
- log.WithContextFields(
- LogFields{
- "reloader": reloader.LogDescription(),
- "reloaded": reloaded}).Info("reload success")
- }
- }
- }
|