From cb15837064bede7490632caae8180341c4ed6768 Mon Sep 17 00:00:00 2001 From: whyrusleeping Date: Fri, 14 Apr 2023 13:05:37 -0700 Subject: [PATCH] fixup auth for invites command --- cmd/gosky/main.go | 113 ++++++++++++++++++++++++++++++++++++++++++---- go.mod | 1 + go.sum | 2 + xrpc/xrpc.go | 2 +- 4 files changed, 108 insertions(+), 10 deletions(-) diff --git a/cmd/gosky/main.go b/cmd/gosky/main.go index f640e06e3..267a9952e 100644 --- a/cmd/gosky/main.go +++ b/cmd/gosky/main.go @@ -10,6 +10,7 @@ import ( "os" "os/signal" "strings" + "sync" "syscall" "time" @@ -276,6 +277,7 @@ var syncCmd = &cli.Command{ Subcommands: []*cli.Command{ syncGetRepoCmd, syncGetRootCmd, + syncListReposCmd, }, } @@ -1011,7 +1013,7 @@ var getRecordCmd = &cli.Command{ } var createInviteCmd = &cli.Command{ - Name: "createInvite", + Name: "createInvites", Flags: []cli.Flag{ &cli.StringFlag{ Name: "admin-password", @@ -1026,6 +1028,9 @@ var createInviteCmd = &cli.Command{ Name: "num", Value: 1, }, + &cli.StringFlag{ + Name: "bulk-dids", + }, }, Action: func(cctx *cli.Context) error { xrpcc, err := cliutil.GetXrpcClient(cctx, false) @@ -1033,32 +1038,122 @@ var createInviteCmd = &cli.Command{ return err } + adminKey := cctx.String("admin-password") + count := cctx.Int("useCount") num := cctx.Int("num") + if bulkfi := cctx.String("bulk-dids"); bulkfi != "" { + xrpcc.AdminToken = &adminKey + dids, err := readDids(bulkfi) + if err != nil { + return err + } + + feeder := make(chan string) + var wg sync.WaitGroup + for i := 0; i < 20; i++ { + wg.Add(1) + go func() { + defer wg.Done() + for d := range feeder { + did := d + resp, err := comatproto.ServerCreateInviteCodes(context.TODO(), xrpcc, &comatproto.ServerCreateInviteCodes_Input{ + UseCount: int64(count), + ForAccount: &did, + CodeCount: int64(num), + }) + if err != nil { + log.Error(err) + } + _ = resp + } + }() + } + + for _, d := range dids { + feeder <- d + } + + close(feeder) + + wg.Wait() + return nil + } + var usrdid *string if forUser := cctx.Args().Get(0); forUser != "" { resp, err := comatproto.IdentityResolveHandle(context.TODO(), xrpcc, forUser) if err != nil { - return err + return fmt.Errorf("resolving handle: %w", err) } usrdid = &resp.Did } - adminKey := cctx.String("admin-password") xrpcc.AdminToken = &adminKey + resp, err := comatproto.ServerCreateInviteCodes(context.TODO(), xrpcc, &comatproto.ServerCreateInviteCodes_Input{ + UseCount: int64(count), + ForAccount: usrdid, + CodeCount: int64(num), + }) + if err != nil { + return fmt.Errorf("creating codes: %w", err) + } - for i := 0; i < num; i++ { - resp, err := comatproto.ServerCreateInviteCode(context.TODO(), xrpcc, &comatproto.ServerCreateInviteCode_Input{ - UseCount: int64(count), - ForAccount: usrdid, - }) + for _, c := range resp.Codes { + fmt.Println(c) + } + + return nil + }, +} + +func readDids(f string) ([]string, error) { + fi, err := os.Open(f) + if err != nil { + return nil, err + } + + defer fi.Close() + + scan := bufio.NewScanner(fi) + var out []string + for scan.Scan() { + out = append(out, scan.Text()) + } + + return out, nil +} + +var syncListReposCmd = &cli.Command{ + Name: "listRepos", + Action: func(cctx *cli.Context) error { + xrpcc, err := cliutil.GetXrpcClient(cctx, false) + if err != nil { + return err + } + + var curs string + for { + out, err := comatproto.SyncListRepos(context.TODO(), xrpcc, curs, 1000) if err != nil { return err } - fmt.Println(resp.Code) + if len(out.Repos) == 0 { + break + } + + for _, r := range out.Repos { + fmt.Println(r.Did) + } + + if out.Cursor == nil { + break + } + + curs = *out.Cursor } return nil diff --git a/go.mod b/go.mod index e943f29ec..7d1bef45c 100644 --- a/go.mod +++ b/go.mod @@ -105,6 +105,7 @@ require ( github.com/prometheus/procfs v0.9.0 // indirect github.com/prometheus/statsd_exporter v0.23.1 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect + github.com/sashabaranov/go-openai v1.7.0 // indirect github.com/spaolacci/murmur3 v1.1.0 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasttemplate v1.2.2 // indirect diff --git a/go.sum b/go.sum index cc413d89a..8842004d1 100644 --- a/go.sum +++ b/go.sum @@ -586,6 +586,8 @@ github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZV github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/sashabaranov/go-openai v1.7.0 h1:D1dBXoZhtf/aKNu6WFf0c7Ah2NM30PZ/3Mqly6cZ7fk= +github.com/sashabaranov/go-openai v1.7.0/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= diff --git a/xrpc/xrpc.go b/xrpc/xrpc.go index c6e7a2c5c..171f442af 100644 --- a/xrpc/xrpc.go +++ b/xrpc/xrpc.go @@ -98,7 +98,7 @@ func (c *Client) Do(ctx context.Context, kind XRPCRequestType, inpenc string, me } // use admin auth if we have it configured and are doing a request that requires it - if c.AdminToken != nil && (strings.HasPrefix(method, "com.atproto.admin.") || method == "com.atproto.account.createInviteCode") { + if c.AdminToken != nil && (strings.HasPrefix(method, "com.atproto.admin.") || method == "com.atproto.account.createInviteCode" || method == "com.atproto.server.createInviteCodes") { req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte("admin:"+*c.AdminToken))) } else if c.Auth != nil { req.Header.Set("Authorization", "Bearer "+c.Auth.AccessJwt)