mirror of
https://github.com/asmogo/nws.git
synced 2024-12-12 18:36:22 +00:00
more linting (#42)
This commit is contained in:
parent
129c8fc55a
commit
646495de4c
@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"github.com/asmogo/nws/config"
|
||||
@ -34,18 +35,18 @@ func main() {
|
||||
func updateConfigFlag(cmd *cobra.Command, cfg *config.ExitConfig) error {
|
||||
httpsPort, err := cmd.Flags().GetInt32("port")
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("failed to get https port: %w", err)
|
||||
}
|
||||
httpTarget, err := cmd.Flags().GetString("target")
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("failed to get http target: %w", err)
|
||||
}
|
||||
cfg.HttpsPort = httpsPort
|
||||
cfg.HttpsTarget = httpTarget
|
||||
return nil
|
||||
}
|
||||
|
||||
func startExitNode(cmd *cobra.Command, args []string) {
|
||||
func startExitNode(cmd *cobra.Command, _ []string) {
|
||||
slog.Info("Starting exit node")
|
||||
// load the configuration
|
||||
cfg, err := config.LoadConfig[config.ExitConfig]()
|
||||
@ -65,17 +66,14 @@ func startExitNode(cmd *cobra.Command, args []string) {
|
||||
exitNode.ListenAndServe(ctx)
|
||||
}
|
||||
|
||||
func startEntryNode(cmd *cobra.Command, args []string) {
|
||||
func startEntryNode(cmd *cobra.Command, _ []string) {
|
||||
slog.Info("Starting entry node")
|
||||
cfg, err := config.LoadConfig[config.EntryConfig]()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// create a new gw server
|
||||
// and start it
|
||||
socksProxy := proxy.New(cmd.Context(), cfg)
|
||||
|
||||
err = socksProxy.Start()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
@ -3,12 +3,14 @@ package protocol
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/google/uuid"
|
||||
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type MessageType string
|
||||
|
||||
var (
|
||||
MessageTypeSocks5 = MessageType("SOCKS5")
|
||||
const (
|
||||
MessageTypeSocks5 = MessageType("SOCKS5RESPONSE")
|
||||
MessageConnect = MessageType("CONNECT")
|
||||
MessageConnectReverse = MessageType("CONNECTR")
|
||||
)
|
||||
@ -60,13 +62,17 @@ func NewMessage(configs ...MessageOption) *Message {
|
||||
return m
|
||||
}
|
||||
func MarshalJSON(m *Message) ([]byte, error) {
|
||||
return json.Marshal(m)
|
||||
data, err := json.Marshal(m)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not marshal message: %w", err)
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func UnmarshalJSON(data []byte) (*Message, error) {
|
||||
m := NewMessage()
|
||||
if err := json.Unmarshal(data, &m); err != nil {
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("could not unmarshal message: %w", err)
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
@ -2,8 +2,6 @@ package protocol
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"github.com/nbd-wtf/go-nostr"
|
||||
"github.com/nbd-wtf/go-nostr/nip04"
|
||||
)
|
||||
@ -22,17 +20,16 @@ const KindPrivateKeyEvent int = 38335
|
||||
|
||||
// EventSigner represents a signer that can create and sign events.
|
||||
//
|
||||
// EventSigner provides methods for creating unsigned events, creating signed events
|
||||
// EventSigner provides methods for creating unsigned events, creating signed events.
|
||||
type EventSigner struct {
|
||||
PublicKey string
|
||||
privateKey string
|
||||
}
|
||||
|
||||
// NewEventSigner creates a new EventSigner
|
||||
// NewEventSigner creates a new EventSigner.
|
||||
func NewEventSigner(privateKey string) (*EventSigner, error) {
|
||||
myPublicKey, err := nostr.GetPublicKey(privateKey)
|
||||
if err != nil {
|
||||
slog.Error("could not generate pubkey")
|
||||
return nil, fmt.Errorf("could not generate public key: %w", err)
|
||||
}
|
||||
signer := &EventSigner{
|
||||
@ -43,7 +40,7 @@ func NewEventSigner(privateKey string) (*EventSigner, error) {
|
||||
}
|
||||
|
||||
// CreateEvent creates a new Event with the provided tags. The Public Key and the
|
||||
// current timestamp are set automatically. The Kind is set to KindEphemeralEvent
|
||||
// current timestamp are set automatically. The Kind is set to KindEphemeralEvent.
|
||||
func (s *EventSigner) CreateEvent(kind int, tags nostr.Tags) nostr.Event {
|
||||
return nostr.Event{
|
||||
PubKey: s.PublicKey,
|
||||
@ -60,7 +57,7 @@ func (s *EventSigner) CreateEvent(kind int, tags nostr.Tags) nostr.Event {
|
||||
// The method then calls CreateEvent to create a new unsigned event with the provided tags.
|
||||
// The encrypted message is set as the content of the event.
|
||||
// Finally, the event is signed with the private key of the EventSigner, setting the event ID and event Sig fields.
|
||||
// The signed event is returned along with any error that occurs
|
||||
// The signed event is returned along with any error that occurs.
|
||||
func (s *EventSigner) CreateSignedEvent(
|
||||
targetPublicKey string,
|
||||
kind int,
|
||||
|
@ -2,53 +2,35 @@ package proxy
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"github.com/asmogo/nws/config"
|
||||
"github.com/asmogo/nws/netstr"
|
||||
"github.com/asmogo/nws/socks5"
|
||||
"github.com/nbd-wtf/go-nostr"
|
||||
"net"
|
||||
)
|
||||
|
||||
type Proxy struct {
|
||||
config *config.EntryConfig // the configuration for the gateway
|
||||
// a list of nostr relays to publish events to
|
||||
relays []*nostr.Relay // deprecated -- should be used for default relay configuration
|
||||
pool *nostr.SimplePool
|
||||
socksServer *socks5.Server
|
||||
}
|
||||
|
||||
func New(ctx context.Context, config *config.EntryConfig) *Proxy {
|
||||
s := &Proxy{
|
||||
proxy := &Proxy{
|
||||
config: config,
|
||||
pool: nostr.NewSimplePool(ctx),
|
||||
}
|
||||
socksServer, err := socks5.New(&socks5.Config{
|
||||
AuthMethods: nil,
|
||||
Credentials: nil,
|
||||
Resolver: netstr.NewNostrDNS(s.pool, config.NostrRelays),
|
||||
Rules: nil,
|
||||
Rewriter: nil,
|
||||
BindIP: net.IP{0, 0, 0, 0},
|
||||
Logger: nil,
|
||||
Dial: nil,
|
||||
}, s.pool, config)
|
||||
Resolver: netstr.NewNostrDNS(proxy.pool, config.NostrRelays),
|
||||
BindIP: net.IP{0, 0, 0, 0},
|
||||
}, proxy.pool, config)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
s.socksServer = socksServer
|
||||
// publish the event to two relays
|
||||
for _, relayUrl := range config.NostrRelays {
|
||||
|
||||
relay, err := s.pool.EnsureRelay(relayUrl)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
continue
|
||||
}
|
||||
s.relays = append(s.relays, relay)
|
||||
fmt.Printf("added relay connection to %s\n", relayUrl)
|
||||
}
|
||||
return s
|
||||
proxy.socksServer = socksServer
|
||||
return proxy
|
||||
}
|
||||
|
||||
// Start should start the server
|
||||
|
@ -374,7 +374,10 @@ func SendReply(w io.Writer, resp uint8, addr *AddrSpec) error {
|
||||
|
||||
// Send the message
|
||||
_, err := w.Write(msg)
|
||||
return err
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to send reply: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type closeWriter interface {
|
||||
@ -385,16 +388,20 @@ type closeWriter interface {
|
||||
// down a dedicated channel
|
||||
func Proxy(dst io.Writer, src io.Reader, errCh chan error) {
|
||||
_, err := io.Copy(dst, src)
|
||||
|
||||
checkError(errCh, err)
|
||||
if tcpConn, ok := dst.(closeWriter); ok {
|
||||
tcpConn.CloseWrite()
|
||||
err = tcpConn.CloseWrite()
|
||||
}
|
||||
if conn, ok := dst.(io.Closer); ok {
|
||||
conn.Close()
|
||||
err = conn.Close()
|
||||
}
|
||||
if conn, ok := src.(io.Closer); ok {
|
||||
conn.Close()
|
||||
err = conn.Close()
|
||||
}
|
||||
checkError(errCh, err)
|
||||
}
|
||||
|
||||
func checkError(errCh chan error, err error) {
|
||||
if errCh != nil {
|
||||
errCh <- err
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user