Antonio Cheong commited on
Commit
b25c140
·
1 Parent(s): 2292b84

Use TLS client

Browse files
Files changed (1) hide show
  1. src/EdgeGPT.py +51 -49
src/EdgeGPT.py CHANGED
@@ -8,6 +8,7 @@ import os
8
  import sys
9
 
10
  import requests
 
11
  import websockets.client as websockets
12
 
13
  DELIMITER = "\x1e"
@@ -104,56 +105,57 @@ class Conversation:
104
  "conversationSignature": None,
105
  "result": {"value": "Success", "message": None},
106
  }
 
107
  # POST request to get token
108
  # Create cookies
109
- if os.environ.get("BING_U") is None:
110
- home = os.path.expanduser("~")
111
- # Check if token exists
112
- token_path = f"{home}/.config/bing_token"
113
- # Make .config directory if it doesn't exist
114
- if not os.path.exists(f"{home}/.config"):
115
- os.mkdir(f"{home}/.config")
116
- if os.path.exists(token_path):
117
- with open(token_path, "r", encoding="utf-8") as file:
118
- token = file.read()
119
- else:
120
- url = "https://images.duti.tech/allow"
121
- response = requests.post(
122
- url,
123
- timeout=10,
124
- headers=headers,
125
- )
126
- if response.status_code != 200:
127
- raise Exception("Authentication failed")
128
- token = response.json()["token"]
129
- # Save token
130
- with open(token_path, "w", encoding="utf-8") as file:
131
- file.write(token)
132
- url = "https://images.duti.tech/auth"
133
- # Send GET request
134
- response = requests.get(
135
- url,
136
- headers=headers,
137
- timeout=10,
138
- )
139
- if response.status_code != 200:
140
- raise Exception("Authentication failed")
141
-
142
- else:
143
- cookies = {
144
- "_U": os.environ.get("BING_U"),
145
- "KievRPSSecAuth": os.environ.get("KIEV_U"),
146
- }
147
- url = "https://www.bing.com/turing/conversation/create"
148
- # Send GET request
149
- response = requests.get(
150
- url,
151
- cookies=cookies,
152
- timeout=30,
153
- headers=headers,
154
- )
155
- if response.status_code != 200:
156
- raise Exception("Authentication failed")
157
  try:
158
  self.struct = response.json()
159
  if self.struct["result"]["value"] == "UnauthorizedRequest":
@@ -350,7 +352,7 @@ if __name__ == "__main__":
350
  parser = argparse.ArgumentParser()
351
  parser.add_argument("--no-stream", action="store_true")
352
  parser.add_argument("--bing-cookie", type=str, default="", required=True)
353
- parser.add_argument("--kiev-cookie", type=str, default="", required=True)
354
  args = parser.parse_args()
355
  if args.bing_cookie:
356
  os.environ["BING_U"] = args.bing_cookie
 
8
  import sys
9
 
10
  import requests
11
+ import tls_client
12
  import websockets.client as websockets
13
 
14
  DELIMITER = "\x1e"
 
105
  "conversationSignature": None,
106
  "result": {"value": "Success", "message": None},
107
  }
108
+ self.session = tls_client.Session(client_identifier="chrome_108")
109
  # POST request to get token
110
  # Create cookies
111
+ # if os.environ.get("BING_U") is None:
112
+ # home = os.path.expanduser("~")
113
+ # # Check if token exists
114
+ # token_path = f"{home}/.config/bing_token"
115
+ # # Make .config directory if it doesn't exist
116
+ # if not os.path.exists(f"{home}/.config"):
117
+ # os.mkdir(f"{home}/.config")
118
+ # if os.path.exists(token_path):
119
+ # with open(token_path, "r", encoding="utf-8") as file:
120
+ # token = file.read()
121
+ # else:
122
+ # url = "https://images.duti.tech/allow"
123
+ # response = requests.post(
124
+ # url,
125
+ # timeout=10,
126
+ # headers=headers,
127
+ # )
128
+ # if response.status_code != 200:
129
+ # raise Exception("Authentication failed")
130
+ # token = response.json()["token"]
131
+ # # Save token
132
+ # with open(token_path, "w", encoding="utf-8") as file:
133
+ # file.write(token)
134
+ # url = "https://images.duti.tech/auth"
135
+ # # Send GET request
136
+ # response = requests.get(
137
+ # url,
138
+ # headers=headers,
139
+ # timeout=10,
140
+ # )
141
+ # if response.status_code != 200:
142
+ # raise Exception("Authentication failed")
143
+
144
+ # else:
145
+ cookies = {
146
+ "_U": os.environ.get("BING_U"),
147
+ "KievRPSSecAuth": os.environ.get("KIEV_U"),
148
+ }
149
+ url = "https://www.bing.com/turing/conversation/create"
150
+ # Send GET request
151
+ response = self.session.get(
152
+ url,
153
+ cookies=cookies,
154
+ timeout_seconds=30,
155
+ headers=headers,
156
+ )
157
+ if response.status_code != 200:
158
+ raise Exception("Authentication failed")
159
  try:
160
  self.struct = response.json()
161
  if self.struct["result"]["value"] == "UnauthorizedRequest":
 
352
  parser = argparse.ArgumentParser()
353
  parser.add_argument("--no-stream", action="store_true")
354
  parser.add_argument("--bing-cookie", type=str, default="", required=True)
355
+ parser.add_argument("--kiev-cookie", type=str, default="", required=False)
356
  args = parser.parse_args()
357
  if args.bing_cookie:
358
  os.environ["BING_U"] = args.bing_cookie