Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(devtools): added go functions to get current network configurati…
…on and apply updates
- Loading branch information
Showing
3 changed files
with
176 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package core | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
account "berty.tech/core/manager/account" | ||
) | ||
|
||
type networkConfig struct { | ||
DefaultTransport bool | ||
BluetoothTransport bool | ||
DefaultBootstrap bool | ||
CustomBootstrap []string | ||
MDNS bool | ||
Relay bool | ||
} | ||
|
||
var currentNetworkConfig = networkConfig{ | ||
DefaultTransport: true, | ||
BluetoothTransport: false, | ||
DefaultBootstrap: true, | ||
CustomBootstrap: []string{}, | ||
MDNS: false, | ||
Relay: false, | ||
} | ||
|
||
func createNetworkConfig() *account.P2PNetworkOptions { | ||
var transport []string | ||
|
||
if currentNetworkConfig.DefaultTransport { | ||
transport = append(transport, "default") | ||
} | ||
if currentNetworkConfig.BluetoothTransport { | ||
transport = append(transport, "ble") | ||
} | ||
|
||
return &account.P2PNetworkOptions{ | ||
Bind: []string{"/ip4/0.0.0.0/tcp/0", "/ble/00000000-0000-0000-0000-000000000000"}, | ||
Transport: transport, | ||
Bootstrap: currentNetworkConfig.CustomBootstrap, | ||
DefaultBootstrap: currentNetworkConfig.DefaultBootstrap, | ||
MDNS: currentNetworkConfig.MDNS, | ||
Relay: currentNetworkConfig.Relay, | ||
Metrics: true, | ||
Identity: "", | ||
} | ||
} | ||
|
||
func GetNetworkConfig() (string, error) { | ||
json, err := json.Marshal(currentNetworkConfig) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return string(json), nil | ||
} | ||
|
||
func UpdateNetworkConfig(jsonConf string) error { | ||
currentAccount, _ := account.Get(accountName) | ||
if currentAccount == nil { | ||
return errors.New("account not running") | ||
} | ||
|
||
var newNetworkConfig networkConfig | ||
|
||
if err := json.Unmarshal([]byte(jsonConf), &newNetworkConfig); err != nil { | ||
return err | ||
} | ||
|
||
currentNetworkConfig = newNetworkConfig | ||
|
||
if err := currentAccount.UpdateP2PNetwork(createNetworkConfig()); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters