From b8cce0cc7e7eef397c9e35f670e92905300efc03 Mon Sep 17 00:00:00 2001 From: Kai Luo Date: Fri, 26 Apr 2024 12:02:33 +0800 Subject: [PATCH] Add generic relayer interface --- relayer/relayer.go | 47 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 relayer/relayer.go diff --git a/relayer/relayer.go b/relayer/relayer.go new file mode 100644 index 0000000..c26f1cd --- /dev/null +++ b/relayer/relayer.go @@ -0,0 +1,47 @@ +// Copyright (c) 2024 Kai Luo . All rights reserved. + +package relayer + +import ( + "log" + "net" + + "github.com/bzEq/bx/core" +) + +type Options struct { + LocalAddr string + NextHops []string +} + +type Relayer struct { + fe Frontend + be Backend +} + +type Frontend interface { + Accept() (core.Port, net.Addr, error) +} + +type Backend interface { + Dial(addr net.Addr) (core.Port, error) +} + +func (self *Relayer) Relay() error { + for { + fp, addr, err := self.fe.Accept() + if err != nil { + return err + } + go func(fp core.Port, addr net.Addr) { + defer fp.Close() + bp, err := self.be.Dial(addr) + if err != nil { + log.Println(err) + return + } + defer bp.Close() + core.RunSimpleSwitch(fp, bp) + }(fp, addr) + } +}