mirror of
https://github.com/go-i2p/goSam.git
synced 2025-06-09 01:25:33 -04:00

- Replace fmt.ErrorF() with errors.New() where no actual formatting requested - Hanle edge cases with no username/password in `Client.hello()` - Make go-staticcheck happier
70 lines
1.4 KiB
Go
70 lines
1.4 KiB
Go
package gosam
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"net"
|
|
"os"
|
|
)
|
|
|
|
// Lookup askes SAM for the internal i2p address from name
|
|
func (c *Client) Lookup(name string) (string, error) {
|
|
r, err := c.sendCmd("NAMING LOOKUP NAME=%s\n", name)
|
|
if err != nil {
|
|
return "", nil
|
|
}
|
|
|
|
if !r.IsOk() {
|
|
return "", ReplyError{r.GetResult(), r}
|
|
}
|
|
|
|
if r.Pairs["NAME"] != name {
|
|
// somehow different on i2pd
|
|
if r.Pairs["NAME"] != "ME" {
|
|
return "", fmt.Errorf("Lookup() Replyed to another name.\nWanted:%s\nGot: %+v", name, r)
|
|
}
|
|
fmt.Fprintln(os.Stderr, "WARNING: Lookup() Replyed to another name. assuming i2pd c++ fluke")
|
|
}
|
|
|
|
return r.Pairs["VALUE"], nil
|
|
}
|
|
|
|
func (c *Client) forward(client, conn net.Conn) {
|
|
defer client.Close()
|
|
defer conn.Close()
|
|
go func() {
|
|
// defer client.Close()
|
|
// defer conn.Close()
|
|
io.Copy(client, conn)
|
|
}()
|
|
go func() {
|
|
// defer client.Close()
|
|
// defer conn.Close()
|
|
io.Copy(conn, client)
|
|
}()
|
|
}
|
|
|
|
func (c *Client) Resolve(ctx context.Context, name string) (context.Context, net.IP, error) {
|
|
// if c.lastaddr == "invalid" || c.lastaddr != name {
|
|
client, err := c.DialContext(ctx, "", name)
|
|
if err != nil {
|
|
return ctx, nil, err
|
|
}
|
|
ln, err := net.Listen("tcp", "127.0.0.1:")
|
|
if err != nil {
|
|
return ctx, nil, err
|
|
}
|
|
go func() {
|
|
for {
|
|
conn, err := ln.Accept()
|
|
if err != nil {
|
|
fmt.Println(err.Error())
|
|
}
|
|
go c.forward(client, conn)
|
|
}
|
|
}()
|
|
// }
|
|
return ctx, nil, nil
|
|
}
|