Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

Commit

Permalink
load tokens from file
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonio committed Apr 11, 2023
1 parent eb4044a commit f5f652d
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ tools/authenticator/accounts.txt
tools/authenticator/proxies.txt
tools/authenticator/authenticated_accounts.txt
tools/authenticator/access_tokens.txt
*.txt
*.txt
access_tokens.json
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.20
require (
github.com/bogdanfinn/fhttp v0.5.19
github.com/bogdanfinn/tls-client v1.3.8
github.com/fvbock/endless v0.0.0-20170109170031-447134032cb6
github.com/gin-gonic/gin v1.9.0
github.com/google/uuid v1.3.0
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583j
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fvbock/endless v0.0.0-20170109170031-447134032cb6 h1:6VSn3hB5U5GeA6kQw4TwWIWbOhtvR2hmbBJnTOtqTWc=
github.com/fvbock/endless v0.0.0-20170109170031-447134032cb6/go.mod h1:YxOVT5+yHzKvwhsiSIWmbAYM3Dr9AEEbER2dVayfBkg=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.9.0 h1:OjyFBKICoexlu99ctXNR2gg+c5pKrKMuyjgARg9qeY8=
Expand Down
21 changes: 21 additions & 0 deletions internal/tokens/tokens.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package tokens

import (
"encoding/json"
"os"
"sync"
)

Expand All @@ -10,6 +12,25 @@ type AccessToken struct {
}

func NewAccessToken(tokens []string) AccessToken {
// Save the tokens to a file
if _, err := os.Stat("access_tokens.json"); os.IsNotExist(err) {
// Create the file
file, err := os.Create("access_tokens.json")
if err != nil {
return AccessToken{}
}
defer file.Close()
}
file, err := os.OpenFile("access_tokens.json", os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
return AccessToken{}
}
defer file.Close()
encoder := json.NewEncoder(file)
err = encoder.Encode(tokens)
if err != nil {
return AccessToken{}
}
return AccessToken{
tokens: tokens,
}
Expand Down
27 changes: 26 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package main

import (
"encoding/json"
"freechatgpt/internal/tokens"
"os"

"github.com/fvbock/endless"
"github.com/gin-gonic/gin"
)

Expand All @@ -22,6 +24,29 @@ func init() {
if PORT == "" {
PORT = "8080"
}
// Check if access_tokens.json exists
if _, err := os.Stat("access_tokens.json"); os.IsNotExist(err) {
// Create the file
file, err := os.Create("access_tokens.json")
if err != nil {
panic(err)
}
defer file.Close()
} else {
// Load the tokens
file, err := os.Open("access_tokens.json")
if err != nil {
panic(err)
}
defer file.Close()
decoder := json.NewDecoder(file)
var token_list []string
err = decoder.Decode(&token_list)
if err != nil {
panic(err)
}
ACCESS_TOKENS = tokens.NewAccessToken(token_list)
}
}

func main() {
Expand All @@ -45,5 +70,5 @@ func main() {
/// Public routes
router.OPTIONS("/v1/chat/completions", optionsHandler)
router.POST("/v1/chat/completions", nightmare)
router.Run(HOST + ":" + PORT)
endless.ListenAndServe(HOST+":"+PORT, router)
}

0 comments on commit f5f652d

Please sign in to comment.