Changeset 385 in code


Ignore:
Timestamp:
Aug 11, 2020, 8:59:06 AM (5 years ago)
Author:
contact
Message:

Add an ident server

Closes: https://todo.sr.ht/~emersion/soju/69

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/cmd/soju/main.go

    r323 r385  
    125125                                log.Fatal(httpSrv.ListenAndServe())
    126126                        }()
     127                case "ident":
     128                        if srv.Identd == nil {
     129                                srv.Identd = soju.NewIdentd()
     130                        }
     131
     132                        host := u.Host
     133                        if _, _, err := net.SplitHostPort(host); err != nil {
     134                                host = host + ":113"
     135                        }
     136                        ln, err := net.Listen("tcp", host)
     137                        if err != nil {
     138                                log.Fatalf("failed to start listener on %q: %v", listen, err)
     139                        }
     140                        go func() {
     141                                log.Fatal(srv.Identd.Serve(ln))
     142                        }()
    127143                default:
    128144                        log.Fatalf("failed to listen on %q: unsupported scheme", listen)
  • trunk/server.go

    r378 r385  
    5252        HTTPOrigins    []string
    5353        AcceptProxyIPs config.IPSet
     54        Identd         *Identd // can be nil
    5455
    5556        db *DB
  • trunk/user.go

    r377 r385  
    22
    33import (
     4        "crypto/sha256"
     5        "encoding/base64"
     6        "encoding/binary"
    47        "fmt"
    58        "time"
     
    100103}
    101104
     105func userIdent(u *User) string {
     106        // The ident is a string we will send to upstream servers in clear-text.
     107        // For privacy reasons, make sure it doesn't expose any meaningful user
     108        // metadata. We just use the base64-encoded hashed ID, so that people don't
     109        // start relying on the string being an integer or following a pattern.
     110        var b [64]byte
     111        binary.LittleEndian.PutUint64(b[:], uint64(u.ID))
     112        h := sha256.Sum256(b[:])
     113        return base64.RawStdEncoding.EncodeToString(h[:])
     114}
     115
    102116func (net *network) run() {
    103117        var lastTry time.Time
     
    119133                        net.user.events <- eventUpstreamConnectionError{net, fmt.Errorf("failed to connect: %v", err)}
    120134                        continue
     135                }
     136
     137                if net.user.srv.Identd != nil {
     138                        net.user.srv.Identd.Store(uc.RemoteAddr().String(), uc.LocalAddr().String(), userIdent(&net.user.User))
    121139                }
    122140
     
    139157                uc.Close()
    140158                net.user.events <- eventUpstreamDisconnected{uc}
     159
     160                if net.user.srv.Identd != nil {
     161                        net.user.srv.Identd.Delete(uc.RemoteAddr().String(), uc.LocalAddr().String())
     162                }
    141163        }
    142164}
Note: See TracChangeset for help on using the changeset viewer.