github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/services/openvpn/service/factory.go (about)

     1  /*
     2   * Copyright (C) 2017 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package service
    19  
    20  import (
    21  	"crypto/x509/pkix"
    22  
    23  	"github.com/rs/zerolog/log"
    24  
    25  	"github.com/mysteriumnetwork/go-openvpn/openvpn/tls"
    26  	"github.com/mysteriumnetwork/node/core/ip"
    27  	"github.com/mysteriumnetwork/node/core/node"
    28  	"github.com/mysteriumnetwork/node/core/port"
    29  	"github.com/mysteriumnetwork/node/eventbus"
    30  	"github.com/mysteriumnetwork/node/firewall"
    31  	"github.com/mysteriumnetwork/node/nat"
    32  )
    33  
    34  // NewManager creates new instance of Openvpn service
    35  func NewManager(nodeOptions node.Options,
    36  	serviceOptions Options,
    37  	country string,
    38  	ipResolver ip.Resolver,
    39  	sessionMap SessionMap,
    40  	natService nat.NATService,
    41  	portPool port.ServicePortSupplier,
    42  	bus eventbus.EventBus,
    43  	trafficFirewall firewall.IncomingTrafficFirewall,
    44  ) *Manager {
    45  	return &Manager{
    46  		nodeOptions:     nodeOptions,
    47  		serviceOptions:  serviceOptions,
    48  		natService:      natService,
    49  		ports:           portPool,
    50  		bus:             bus,
    51  		trafficFirewall: trafficFirewall,
    52  		country:         country,
    53  		ipResolver:      ipResolver,
    54  
    55  		openvpnClients: NewClientMap(sessionMap),
    56  	}
    57  }
    58  
    59  func vpnServerIP(outboundIP, publicIP string, isLocalnet bool) string {
    60  	if publicIP == outboundIP {
    61  		return publicIP
    62  	}
    63  
    64  	if isLocalnet {
    65  		log.Warn().Msgf(
    66  			`WARNING: It seems that publicly visible ip: [%s] does not match your local machines ip: [%s].
    67  Since it's localnet, will use %v for openvpn service`, publicIP,
    68  			outboundIP,
    69  			outboundIP)
    70  		return outboundIP
    71  	}
    72  
    73  	return publicIP
    74  }
    75  
    76  // primitiveFactory takes in the country and providerID and forms the tls primitives out of it
    77  func primitiveFactory(currentCountry, providerID string) (*tls.Primitives, error) {
    78  	log.Info().Msg("Country detected: " + currentCountry)
    79  
    80  	caSubject := pkix.Name{
    81  		Country:            []string{currentCountry},
    82  		Organization:       []string{"Mysterium Network"},
    83  		OrganizationalUnit: []string{"Mysterium Team"},
    84  	}
    85  	serverCertSubject := pkix.Name{
    86  		Country:            []string{currentCountry},
    87  		Organization:       []string{"Mysterium node operator company"},
    88  		OrganizationalUnit: []string{"Node operator team"},
    89  		CommonName:         providerID,
    90  	}
    91  
    92  	return tls.NewTLSPrimitives(caSubject, serverCertSubject)
    93  }