How can nodes on two different local area networks communicate, and is it necessary to deploy a relay node on the public internet?" Regarding the code, you haven’t provided any specific code to review. If you have some code that you need assistance with,
var gsf *GameShareFS
type GameShareFS struct {
repo repo.Repo
node *core.IpfsNode
api *coreapi.CoreAPI
ctx context.Context
}
func loadPlugins(repoPath string) (err error) {
plugins, err := loader.NewPluginLoader(repoPath)
if err != nil {
return
}
err = plugins.Initialize()
if err != nil {
return
}
err = plugins.Inject()
if err != nil {
return
}
return
}
func StartShareFS(ctx context.Context, repoPath string) (err error) {
if gsf != nil {
return
}
// 加载插件
err = loadPlugins(repoPath)
if err != nil {
return
}
// IPFS 节点的存储位置
// 如果仓库不存在,则进行初始化
if !fsrepo.IsInitialized(repoPath) {
var conf *config.Config
conf, err = config.Init(os.Stdout, 2048)
if err != nil {
return
}
conf.Datastore.StorageMax = StorageMax
conf.Datastore.StorageGCWatermark = StorageGCWatermark
err = fsrepo.Init(repoPath, conf)
if err != nil {
return
}
}
// 创建和打开 IPFS 仓库
repo, err := fsrepo.Open(repoPath)
if err != nil {
return
}
// 读取配置
cfg, err := repo.Config()
if err != nil {
return
}
// 配置新的中继服务(circuit v2)
cfg.Swarm.RelayClient.Enabled = 1
cfg.Swarm.RelayService.Enabled = 1 // 如果你想让你的节点作为中继节点
// 开启NAT穿透
cfg.Swarm.Transports.Network.QUIC = config.True
// 应用并保存修改后的配置
err = repo.SetConfig(cfg)
if err != nil {
return
}
// 创建 IPFS 节点
node, err := core.NewNode(ctx, &core.BuildCfg{
Repo: repo,
Online: true,
ExtraOpts: map[string]bool{
"pubsub": true, // 启用 pubsub
"ipnsps": true, // 启用 IPNS over pubsub
"mplex": true, // 启用 mplex 多路复用
},
})
if err != nil {
return
}
// 获取核心API
api, err := coreapi.NewCoreAPI(node)
if err != nil {
return
}
gsf = &GameShareFS{repo: repo, node: node, api: api.(*coreapi.CoreAPI), ctx: ctx}
return
}
func EndShareFS() {
if gsf != nil {
//gsf.repo.Close()
//gsf.node.Close()
gsf.ctx.Done()
}
}