Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit 53cfc42

Browse files
authored
Merge pull request #1700 from janhq/j/proxy-docs
docs: add proxy docs
2 parents 70e25b7 + 640d66a commit 53cfc42

File tree

6 files changed

+228
-4
lines changed

6 files changed

+228
-4
lines changed

docs/docs/configurations/index.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ title: Cortex configurations
88
Welcome to the Cortex configurations documentation. Here you will find detailed guides and references for configuring various aspects of Cortex, including:
99

1010
- **CORS**: Learn how to set up Cross-Origin Resource Sharing.
11+
- **Proxy**: Configure the proxy for Cortex.
1112

1213
Use the sidebar to navigate through the different configuration topics.

docs/docs/configurations/proxy.mdx

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
---
2+
title: Proxy
3+
description: Setting up Proxy
4+
slug: "proxy"
5+
---
6+
7+
import Tabs from "@theme/Tabs";
8+
import TabItem from "@theme/TabItem";
9+
10+
:::warning
11+
🚧 Cortex.cpp is currently under development. Our documentation outlines the intended behavior of Cortex, which may not yet be fully implemented in the codebase.
12+
:::
13+
14+
# Proxy Configuration Guide
15+
16+
This document describes how to configure proxy settings for Cortex to be able to work behind your proxy.
17+
18+
## Command Line Interface (CLI)
19+
20+
### Basic Usage
21+
22+
```bash
23+
cortex config [OPTIONS] [COMMAND]
24+
```
25+
26+
### Commands
27+
28+
- `status`: Display all current configurations
29+
30+
```bash
31+
cortex config status
32+
```
33+
34+
Example Output:
35+
36+
```bash
37+
+-----------------------+------------------------+
38+
| Config name | Value |
39+
+-----------------------+------------------------+
40+
| no_proxy | localhost,127.0.0.1 |
41+
+-----------------------+------------------------+
42+
| proxy_password | |
43+
+-----------------------+------------------------+
44+
| proxy_url | http://localhost:8080 |
45+
+-----------------------+------------------------+
46+
| proxy_username | |
47+
+-----------------------+------------------------+
48+
| verify_host_ssl | true |
49+
+-----------------------+------------------------+
50+
| verify_peer_ssl | false |
51+
+-----------------------+------------------------+
52+
| verify_proxy_host_ssl | true |
53+
+-----------------------+------------------------+
54+
| verify_proxy_ssl | true |
55+
+-----------------------+------------------------+
56+
```
57+
58+
### Options
59+
60+
| Option | Description | Example |
61+
| ----------------------------------- | --------------------------- | ------------------------------------------------- |
62+
| `-h, --help` | Print help message and exit |
63+
| `--proxy_url <proxy_url>` | Set the proxy URL | `cortex config --proxy_url http://localhost:8080` |
64+
| `--proxy_username <proxy_username>` | Set the username for proxy | `cortex config --proxy_username my_username` |
65+
| `--proxy_password <proxy_password>` | Set the password for proxy | `cortex config --proxy_password my_password` |
66+
| `--no_proxy <no_proxy>` | Set the no_proxy list | `cortex config --no_proxy localhost,127.0.0.1` |
67+
| `--verify_proxy_ssl [on/off]` | Verify proxy SSL | `cortex config --verify_proxy_ssl on` |
68+
| `--verify_proxy_host_ssl [on/off]` | Verify proxy host SSL | `cortex config --verify_proxy_host_ssl on` |
69+
| `--verify_peer_ssl [on/off]` | Verify peer SSL | `cortex config --verify_peer_ssl off` |
70+
| `--verify_host_ssl [on/off]` | Verify host SSL | `cortex config --verify_host_ssl on` |
71+
72+
## Proxy API Configuration
73+
74+
### Endpoints
75+
76+
#### Get Current Configuration
77+
78+
```http
79+
GET /v1/configs
80+
```
81+
82+
Retrieves the current configuration settings.
83+
84+
##### Response
85+
86+
```json
87+
{
88+
"allowed_origins": [
89+
"http://localhost:39281",
90+
"http://127.0.0.1:39281",
91+
"http://0.0.0.0:39281"
92+
],
93+
"cors": true,
94+
"no_proxy": "localhost,127.0.0.1",
95+
"proxy_password": "",
96+
"proxy_url": "http://localhost:8080",
97+
"proxy_username": "",
98+
"verify_host_ssl": true,
99+
"verify_peer_ssl": false,
100+
"verify_proxy_host_ssl": true,
101+
"verify_proxy_ssl": true
102+
}
103+
```
104+
105+
#### Update Configuration
106+
107+
```http
108+
PATCH /v1/configs
109+
```
110+
111+
Updates proxy configuration settings.
112+
113+
##### Request Headers
114+
115+
```
116+
Content-Type: application/json
117+
```
118+
119+
##### Request Body
120+
121+
```json
122+
{
123+
"no_proxy": "localhost",
124+
"proxy_url": "http://localhost:8080",
125+
"proxy_username": "my_username",
126+
"proxy_password": "my_password",
127+
"verify_host_ssl": false,
128+
"verify_peer_ssl": false,
129+
"verify_proxy_host_ssl": false,
130+
"verify_proxy_ssl": false
131+
}
132+
```
133+
134+
##### Parameters
135+
136+
| Field | Type | Description |
137+
| ----------------------- | ------- | -------------------------------------------------------------------------------------- |
138+
| `no_proxy` | string | List of origins which request do not need to go through a proxy. Comma separated value |
139+
| `proxy_url` | string | Proxy URL |
140+
| `proxy_username` | string | Username for proxy authentication |
141+
| `proxy_password` | string | Password for proxy authentication |
142+
| `verify_host_ssl` | boolean | Verify host SSL |
143+
| `verify_peer_ssl` | boolean | Verify peer SSL |
144+
| `verify_proxy_host_ssl` | boolean | Verify proxy host SSL |
145+
| `verify_proxy_ssl` | boolean | Verify proxy SSL |
146+
147+
##### Response
148+
149+
```json
150+
{
151+
"config": {
152+
"allowed_origins": [
153+
"http://localhost:39281",
154+
"http://127.0.0.1:39281",
155+
"http://0.0.0.0:39281"
156+
],
157+
"cors": true,
158+
"no_proxy": "localhost",
159+
"proxy_password": "my_password",
160+
"proxy_url": "http://localhost:8080",
161+
"proxy_username": "my_username",
162+
"verify_host_ssl": false,
163+
"verify_peer_ssl": false,
164+
"verify_proxy_host_ssl": false,
165+
"verify_proxy_ssl": false
166+
},
167+
"message": "Configuration updated successfully"
168+
}
169+
```
170+
171+
## Testing proxy configuration
172+
173+
You can test your proxy configuration using [mitmproxy](https://docs.mitmproxy.org/stable). This guide is written on macOS, but you can use it on any other platform.
174+
175+
### Install mitmproxy
176+
177+
```bash
178+
brew install mitmproxy
179+
```
180+
181+
### Start mitmproxy
182+
183+
```bash
184+
mitmproxy --set stream_large_bodies=1m
185+
```
186+
187+
mitmproxy will start on port `8080`. After mitmproxy started, you can adding options by pressing `O`. mitmproxy will display an option screen. You can check their document to learn more about mitmproxy. But let's take a simple option for now by setting the `proxyauth` for our local proxy. Inside the option screen, search for `proxyauth` and hit enter. Then, type `username:password` and hit enter again. You will see your newly added option is red-colored.
188+
189+
### Configuring Cortex to use that proxy
190+
191+
Let's using CLI to configure Cortex to use that proxy.
192+
193+
```bash
194+
cortex config --proxy_url http://localhost:8080 --proxy_username username --proxy_password password
195+
```
196+
197+
### Testing the proxy
198+
199+
Now, let's test the proxy. If you are setting the username and password correctly (same with `proxyauth` in mitmproxy), you will see the request in mitmproxy. For example, command `cortex pull tinyllama` should be successfully and returns a list of selectable models. Also, you will see your request in mitmproxy CLI screen.
200+
201+
Let's try to use a wrong authentication for your proxy.
202+
203+
```bash
204+
cortex config --proxy_password wrong_pw
205+
```
206+
207+
Now, let's test the proxy again. You will see the request is failed and returns an error.

docs/sidebars.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ const sidebars: SidebarsConfig = {
9595
id: "configurations/cors",
9696
label: "CORS",
9797
},
98+
{
99+
type: "doc",
100+
id: "configurations/proxy",
101+
label: "Proxy",
102+
},
98103
],
99104
},
100105
{

engine/services/download_service.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "utils/format_utils.h"
1111
#include "utils/logging_utils.h"
1212
#include "utils/result.hpp"
13+
#include "utils/string_utils.h"
1314

1415
namespace {
1516
size_t WriteCallback(char* ptr, size_t size, size_t nmemb, void* userdata) {
@@ -75,6 +76,10 @@ void SetUpProxy(CURL* handle, std::shared_ptr<ConfigService> config_service) {
7576
CTL_INF("Verify host ssl: " << verify_host_ssl);
7677

7778
curl_easy_setopt(handle, CURLOPT_PROXY, proxy_url.c_str());
79+
if (string_utils::StartsWith(proxy_url, "https")) {
80+
curl_easy_setopt(handle, CURLOPT_PROXYTYPE, CURLPROXY_HTTPS);
81+
}
82+
7883
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, verify_ssl ? 1L : 0L);
7984
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST,
8085
verify_host_ssl ? 2L : 0L);

engine/utils/config_yaml_utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ constexpr const auto kDefaultLatestLlamacppRelease = "";
5757
constexpr const auto kDefaultCorsEnabled = true;
5858
const std::vector<std::string> kDefaultEnabledOrigins{
5959
"http://localhost:39281", "http://127.0.0.1:39281", "http://0.0.0.0:39281"};
60-
constexpr const auto kDefaultNoProxy = "localhost,127.0.0.1";
60+
constexpr const auto kDefaultNoProxy = "example.com,::1,localhost,127.0.0.1";
6161

6262
inline cpp::result<void, std::string> DumpYamlConfig(const CortexConfig& config,
6363
const std::string& path) {

engine/utils/curl_utils.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "utils/file_manager_utils.h"
1111
#include "utils/logging_utils.h"
1212
#include "utils/result.hpp"
13+
#include "utils/string_utils.h"
1314
#include "utils/url_parser.h"
1415

1516
enum class RequestType { GET, PATCH, POST, DEL };
@@ -24,7 +25,7 @@ size_t WriteCallback(void* contents, size_t size, size_t nmemb,
2425
return totalSize;
2526
}
2627

27-
void SetUpProxy(CURL* handle) {
28+
void SetUpProxy(CURL* handle, const std::string& url) {
2829
auto config = file_manager_utils::GetCortexConfig();
2930
if (!config.proxyUrl.empty()) {
3031
auto proxy_url = config.proxyUrl;
@@ -39,13 +40,18 @@ void SetUpProxy(CURL* handle) {
3940
auto no_proxy = config.noProxy;
4041

4142
CTL_INF("=== Proxy configuration ===");
43+
CTL_INF("Request url: " << url);
4244
CTL_INF("Proxy url: " << proxy_url);
4345
CTL_INF("Verify proxy ssl: " << verify_proxy_ssl);
4446
CTL_INF("Verify proxy host ssl: " << verify_proxy_host_ssl);
4547
CTL_INF("Verify ssl: " << verify_ssl);
4648
CTL_INF("Verify host ssl: " << verify_host_ssl);
49+
CTL_INF("No proxy: " << no_proxy);
4750

4851
curl_easy_setopt(handle, CURLOPT_PROXY, proxy_url.c_str());
52+
if (string_utils::StartsWith(proxy_url, "https")) {
53+
curl_easy_setopt(handle, CURLOPT_PROXYTYPE, CURLPROXY_HTTPS);
54+
}
4955
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, verify_ssl ? 1L : 0L);
5056
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, verify_host_ssl ? 2L : 0L);
5157

@@ -88,7 +94,7 @@ inline cpp::result<std::string, std::string> SimpleGet(const std::string& url,
8894

8995
std::string readBuffer;
9096

91-
SetUpProxy(curl);
97+
SetUpProxy(curl, url);
9298
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
9399
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
94100
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
@@ -139,7 +145,7 @@ inline cpp::result<std::string, std::string> SimpleRequest(
139145
}
140146
std::string readBuffer;
141147

142-
SetUpProxy(curl);
148+
SetUpProxy(curl, url);
143149
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, curl_headers);
144150
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
145151
if (request_type == RequestType::PATCH) {

0 commit comments

Comments
 (0)