github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/mobile/mysterium/mobile_provider.go (about) 1 /* 2 * Copyright (C) 2018 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 mysterium 19 20 import ( 21 "encoding/json" 22 "strings" 23 24 "github.com/mysteriumnetwork/node/config" 25 "github.com/mysteriumnetwork/node/core/service/servicestate" 26 "github.com/mysteriumnetwork/node/identity" 27 "github.com/mysteriumnetwork/node/services" 28 "github.com/mysteriumnetwork/node/tequilapi/contract" 29 "github.com/rs/zerolog/log" 30 31 "github.com/mysteriumnetwork/node/services/datatransfer" 32 "github.com/mysteriumnetwork/node/services/dvpn" 33 "github.com/mysteriumnetwork/node/services/scraping" 34 "github.com/mysteriumnetwork/node/services/wireguard" 35 ) 36 37 // DefaultProviderNodeOptions returns default options. 38 func DefaultProviderNodeOptions() *MobileNodeOptions { 39 options := DefaultNodeOptionsByNetwork(string(config.Mainnet)) 40 options.IsProvider = true 41 options.TequilapiSecured = true 42 return options 43 } 44 45 func (mb *MobileNode) unlockIdentity(adr, passphrase string) string { 46 chainID := config.GetInt64(config.FlagChainID) 47 id, err := mb.identitySelector.UseOrCreate(adr, passphrase, chainID) 48 if err != nil { 49 return "" 50 } 51 return id.Address 52 } 53 54 // StartProvider starts all provider services (provider mode) 55 func (mb *MobileNode) StartProvider() { 56 providerID := mb.unlockIdentity( 57 config.FlagIdentity.Value, 58 config.FlagIdentityPassphrase.Value, 59 ) 60 log.Info().Msgf("Unlocked identity: %v", providerID) 61 62 serviceTypes := strings.Split(config.Current.GetString(config.FlagActiveServices.Name), ",") 63 if len(serviceTypes) == 1 && serviceTypes[0] == "" { 64 serviceTypes = []string{} 65 } 66 stoppedServices := strings.Split(config.Current.GetString(config.FlagStoppedServices.Name), ",") 67 if len(stoppedServices) == 1 && stoppedServices[0] == "" { 68 stoppedServices = []string{} 69 } 70 71 if len(serviceTypes) == 0 { 72 if len(stoppedServices) > 0 { 73 74 // restore stopped service 75 serviceTypes = stoppedServices 76 stoppedServices = []string{} 77 } else { 78 79 // on the first run enable data scraping service 80 if config.Current.GetString(contract.TermsProviderAgreed) == "" { 81 serviceTypes = []string{scraping.ServiceType} 82 } 83 } 84 } 85 86 setUserConfigRaw(config.FlagStoppedServices.Name, strings.Join(stoppedServices, ",")) 87 setUserConfigRaw(config.FlagActiveServices.Name, strings.Join(serviceTypes, ",")) 88 config.Current.SaveUserConfig() 89 90 // set of services to be started 91 serviceTypesSet := make(map[string]bool) 92 for _, serviceType := range serviceTypes { 93 serviceTypesSet[serviceType] = true 94 } 95 for _, srv := range mb.servicesManager.List(true) { 96 if srv.State() == servicestate.Running { 97 delete(serviceTypesSet, srv.Type) 98 } 99 } 100 101 for serviceType := range serviceTypesSet { 102 serviceOpts, err := services.GetStartOptions(serviceType) 103 if err != nil { 104 log.Error().Err(err).Msg("GetStartOptions failed") 105 return 106 } 107 108 _, err = mb.servicesManager.Start(identity.Identity{Address: providerID}, serviceType, serviceOpts.AccessPolicyList, serviceOpts.TypeOptions) 109 if err != nil { 110 log.Error().Err(err).Msg("servicesManager.Start failed") 111 return 112 } 113 } 114 } 115 116 // StopProvider stops all provider services, started by StartProvider 117 func (mb *MobileNode) StopProvider() { 118 haveActive := false 119 for _, srv := range mb.servicesManager.List(true) { 120 if srv.State() == servicestate.Running { 121 haveActive = true 122 break 123 } 124 } 125 126 stoppedServices := strings.Split(config.Current.GetString(config.FlagStoppedServices.Name), ",") 127 if len(stoppedServices) == 1 && stoppedServices[0] == "" { 128 stoppedServices = []string{} 129 } 130 131 if haveActive { 132 stoppedServices = []string{} 133 for _, srv := range mb.servicesManager.List(true) { 134 if srv.State() != servicestate.Running { 135 continue 136 } 137 138 stoppedServices = append(stoppedServices, srv.Type) 139 err := mb.servicesManager.Stop(srv.ID) 140 if err != nil { 141 log.Error().Err(err).Msg("servicesManager.Stop failed") 142 return 143 } 144 } 145 } 146 147 setUserConfigRaw(config.FlagStoppedServices.Name, strings.Join(stoppedServices, ",")) 148 setUserConfigRaw(config.FlagActiveServices.Name, "") 149 config.Current.SaveUserConfig() 150 } 151 152 // SetFlagLauncherVersion sets LauncherVersion flag value, which is reported to Prometheus 153 func SetFlagLauncherVersion(val string) { 154 config.Current.SetDefault(config.FlagLauncherVersion.Name, val) 155 } 156 157 func getAllServiceTypes() []string { 158 return []string{wireguard.ServiceType, scraping.ServiceType, datatransfer.ServiceType, dvpn.ServiceType} 159 } 160 161 // GetServiceTypes returns all possible service types 162 func GetServiceTypes() ([]byte, error) { 163 result := getAllServiceTypes() 164 return json.Marshal(&result) 165 } 166 167 // ServicesState - state of service 168 type ServicesState struct { 169 Service string `json:"id"` 170 State string `json:"state"` 171 } 172 173 // GetAllServicesState returns state of all services 174 func (mb *MobileNode) GetAllServicesState() ([]byte, error) { 175 result := make([]ServicesState, 0) 176 for _, srv := range mb.servicesManager.List(true) { 177 result = append(result, ServicesState{srv.Type, string(srv.State())}) 178 } 179 return json.Marshal(&result) 180 }