Skip to content
This repository has been archived by the owner on Mar 22, 2024. It is now read-only.

Commit

Permalink
update vscode machineid and sessionid store
Browse files Browse the repository at this point in the history
  • Loading branch information
qi.zhong committed Mar 6, 2024
1 parent 0f6b4e3 commit 574bd84
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 25 deletions.
41 changes: 29 additions & 12 deletions copilot.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
Expand All @@ -11,6 +13,7 @@ import (
"sync"
"time"

"github.com/google/uuid"
"github.com/mitchellh/go-homedir"
"github.com/patrickmn/go-cache"
"github.com/tidwall/gjson"
Expand All @@ -35,6 +38,12 @@ type copilot struct {
} `json:"github.com"`
}

type vscodeCopilot struct {
token string
machineid string
sessionid string
}

func Init() {
// init cache
one := &sync.Once{}
Expand Down Expand Up @@ -76,18 +85,18 @@ func Init() {
// Copiloter is the interface that wraps the token method.
// token return the access token for github copilot
type copiloter interface {
token() (string, error)
refresh() (string, error)
token() (*vscodeCopilot, error)
refresh() (*vscodeCopilot, error)
}

func (c *copilot) refresh() (string, error) {
func (c *copilot) refresh() (*vscodeCopilot, error) {
caches.Delete(c.GithubCom.OauthToken)
return c.token()
}

func (c *copilot) token() (string, error) {
func (c *copilot) token() (*vscodeCopilot, error) {
if cacheToken, ok := caches.Get(c.GithubCom.OauthToken); ok {
return cacheToken.(string), nil
return cacheToken.(*vscodeCopilot), nil
}
tokenURL := c.GithubCom.DevOverride.CopilotTokenURL
if tokenURL == "" {
Expand All @@ -96,27 +105,35 @@ func (c *copilot) token() (string, error) {

req, err := http.NewRequest(http.MethodGet, tokenURL, nil)
if err != nil {
return "", err
return nil, err
}
req.Header.Set("Authorization", fmt.Sprintf("token %s", c.GithubCom.OauthToken))

resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
return nil, err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("get token error: %d", resp.StatusCode)
return nil, fmt.Errorf("get token error: %d", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
return nil, err
}
if token := gjson.GetBytes(body, "token").String(); token != "" {
caches.Set(c.GithubCom.OauthToken, token, 14*time.Minute)
return token, nil
sessionId := fmt.Sprintf("%s%d", uuid.New().String(), time.Now().UnixNano()/int64(time.Millisecond))
machineID := sha256.Sum256([]byte(uuid.New().String()))
machineIDStr := hex.EncodeToString(machineID[:])
vscodeCopilot := &vscodeCopilot{
token: token,
machineid: machineIDStr,
sessionid: sessionId,
}
caches.Set(c.GithubCom.OauthToken, vscodeCopilot, 14*time.Minute)
return vscodeCopilot, nil
}

return "", fmt.Errorf("get token error")
return nil, fmt.Errorf("get token error")
}
16 changes: 4 additions & 12 deletions header.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
package main

import (
"crypto/sha256"
"encoding/hex"
"fmt"
"time"

"github.com/google/uuid"
)

func getAccHeaders(accessToken string) map[string]string {
sessionId := fmt.Sprintf("%s%d", uuid.New().String(), time.Now().UnixNano()/int64(time.Millisecond))
machineID := sha256.Sum256([]byte(uuid.New().String()))
machineIDStr := hex.EncodeToString(machineID[:])
func getAccHeaders(accessToken *vscodeCopilot) map[string]string {
return map[string]string{
"Host": "api.githubcopilot.com",
"Authorization": "Bearer " + accessToken,
"Authorization": "Bearer " + accessToken.token,
"X-Request-Id": uuid.New().String(),
"X-Github-Api-Version": "2023-07-07",
"Vscode-Sessionid": sessionId,
"Vscode-machineid": machineIDStr,
"Vscode-Sessionid": accessToken.sessionid,
"Vscode-machineid": accessToken.machineid,
"Editor-Version": "vscode/1.85.1",
"Editor-Plugin-Version": "copilot-chat/0.11.1",
"Openai-Organization": "github-copilot",
Expand Down
2 changes: 1 addition & 1 deletion proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func handleProxy(w http.ResponseWriter, r *http.Request) {
if err != nil {
accToken, _ = getCopilot().refresh()
}
if accToken == "" {
if accToken == nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "get acc token error: %v", err)
return
Expand Down

0 comments on commit 574bd84

Please sign in to comment.