Skip to content

Commit 0947305

Browse files
committed
feat: allow using https api
1 parent 32c55bc commit 0947305

File tree

5 files changed

+32
-142
lines changed

5 files changed

+32
-142
lines changed

src/App.tsx

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,7 @@ const App: React.FC = () => {
130130
if (result) {
131131
currentProfile.current = result
132132

133-
if (result.helperHost && result.helperPort) {
134-
setServer(result.host, result.port, result.key, {
135-
helperHost: result.helperHost,
136-
helperPort: result.helperPort,
137-
})
138-
} else {
139-
setServer(result.host, result.port, result.key)
140-
}
133+
setServer(result.host, result.port, result.key, { tls: result.tls })
141134
} else {
142135
history.replace('/')
143136
}

src/components/ProfileCell/index.tsx

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { Bin, PaperPlane } from '@sumup/icons'
99
import { IconButton } from '@sumup/circuit-ui'
1010

1111
import { Profile } from '../../types'
12-
import { bareFetcher } from '../../utils/fetcher'
1312

1413
interface ProfileCellProps {
1514
profile: Profile
@@ -66,25 +65,17 @@ const ProfileCell: React.FC<ProfileCellProps> = ({
6665
let isMounted = true
6766
6867
if (checkConnectivity) {
69-
const options =
70-
profile.helperHost && profile.helperPort
71-
? {
72-
helperHost: profile.helperHost,
73-
helperPort: Number(profile.helperPort),
74-
}
75-
: undefined
76-
77-
bareFetcher(
78-
{
79-
url: `http://${profile.host}:${profile.port}/v1/outbound`,
68+
axios
69+
.request({
70+
url: `${profile.tls ? 'https' : 'http'}://${profile.host}:${
71+
profile.port
72+
}/v1/outbound`,
8073
method: 'GET',
8174
headers: {
8275
'x-key': profile.key,
8376
},
8477
timeout: 3000,
85-
},
86-
options,
87-
)
78+
})
8879
.then(() => {
8980
if (isMounted) setAvailable(true)
9081
})
@@ -124,19 +115,6 @@ const ProfileCell: React.FC<ProfileCellProps> = ({
124115
</div>
125116
)}
126117
<div tw="flex items-center font-mono text-gray-600 text-xs md:text-sm truncate leading-none">
127-
{!!profile.helperHost && (
128-
<div
129-
title={`Helper: ${profile.helperHost}:${profile.helperPort}`}
130-
tw="cursor-pointer">
131-
<PaperPlane
132-
css={css`
133-
width: 1rem;
134-
height: 1rem;
135-
margin-right: 0.25rem;
136-
`}
137-
/>
138-
</div>
139-
)}
140118
<span>
141119
{profile.host}:{profile.port}
142120
</span>

src/pages/Landing/index.tsx

Lines changed: 18 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/** @jsx jsx */
22
import { jsx } from '@emotion/core'
3+
import axios from 'axios'
34
import React, {
45
FormEventHandler,
56
useCallback,
@@ -20,20 +21,15 @@ import Ad from '../../components/Ad'
2021
import useSetState from '../../hooks/use-set-state'
2122
import { Profile } from '../../types'
2223
import { ExistingProfiles, LastUsedProfile } from '../../utils/constant'
23-
import { bareFetcher } from '../../utils/fetcher'
2424

2525
const Page: React.FC = () => {
2626
const history = useHistory()
2727
const protocol = window.location.protocol
2828
const [name, setName] = useState<string | undefined>()
2929
const [host, setHost] = useState<string | undefined>()
30-
const [helperHost, setHelperHost] = useState<string | undefined>()
3130
const [port, setPort] = useState<string | undefined>()
32-
const [helperPort, setHelperPort] = useState<string | undefined>()
3331
const [key, setKey] = useState<string | undefined>()
34-
const [useHelper, setUseHelper] = useState<boolean>(
35-
() => protocol === 'https:',
36-
)
32+
const [useTls, setUseTls] = useState<boolean>(() => protocol === 'https:')
3733
const [
3834
existingProfiles,
3935
setExistingProfiles,
@@ -84,8 +80,7 @@ const Page: React.FC = () => {
8480
setHost('')
8581
setPort('')
8682
setKey('')
87-
setHelperHost('')
88-
setHelperPort('')
83+
setUseTls(protocol === 'https:')
8984
}
9085

9186
const onSubmit: FormEventHandler = (e) => {
@@ -97,25 +92,15 @@ const Page: React.FC = () => {
9792

9893
setIsLoading(true)
9994

100-
const options =
101-
useHelper && helperHost && helperPort
102-
? {
103-
helperHost,
104-
helperPort: Number(helperPort),
105-
}
106-
: undefined
107-
108-
bareFetcher(
109-
{
110-
url: `http://${host}:${port}/v1/outbound`,
95+
axios
96+
.request({
97+
url: `${useTls ? 'https' : 'http'}://${host}:${port}/v1/outbound`,
11198
method: 'GET',
11299
timeout: 3000,
113100
headers: {
114101
'x-key': key,
115102
},
116-
},
117-
options,
118-
)
103+
})
119104
.then((res) => {
120105
setHasError(false)
121106

@@ -129,8 +114,7 @@ const Page: React.FC = () => {
129114
: 'ios',
130115
platformVersion: res.headers['x-surge-version'] || '',
131116
platformBuild: res.headers['x-surge-build'] || '',
132-
helperHost: helperHost,
133-
helperPort: Number(helperPort),
117+
tls: useTls,
134118
})
135119

136120
resetFields()
@@ -184,16 +168,8 @@ const Page: React.FC = () => {
184168
</a>
185169
</p>
186170
<p tw="leading-normal mb-2">
187-
您已可通过 yasd-helper 实现 HTTPS 访问 Surge API。
188-
</p>
189-
<p tw="leading-normal">
190-
<a
191-
href="https://github.com/geekdada/yasd-helper"
192-
target="_blank"
193-
rel="noreferrer"
194-
tw="border-b border-solid border-teal-500">
195-
🔗 查看
196-
</a>
171+
Surge Mac v4.0.6 (1280) 开始已支持开启 HTTPS API,故不再支持使用
172+
yasd-helper。
197173
</p>
198174
</div>
199175

@@ -243,43 +219,16 @@ const Page: React.FC = () => {
243219
setKey((target as HTMLInputElement).value)
244220
}
245221
/>
246-
<div>
247-
<div>
248-
<Checkbox
249-
disabled={protocol === 'https:'}
250-
checked={useHelper}
251-
onChange={() => setUseHelper((prev) => !prev)}>
252-
使用 yasd-helper 中转
253-
</Checkbox>
254-
</div>
255222

256-
{useHelper && (
257-
<React.Fragment>
258-
<Input
259-
type="text"
260-
required={protocol === 'https:'}
261-
invalid={!!hasError}
262-
label="Helper Host"
263-
placeholder="192.168.1.2.nip.io"
264-
value={helperHost}
265-
onChange={({ target }) =>
266-
setHelperHost((target as HTMLInputElement).value)
267-
}
268-
/>
269-
<Input
270-
type="number"
271-
required={protocol === 'https:'}
272-
invalid={!!hasError}
273-
label="Helper Port"
274-
placeholder="8443"
275-
value={helperPort}
276-
onChange={({ target }) =>
277-
setHelperPort((target as HTMLInputElement).value)
278-
}
279-
/>
280-
</React.Fragment>
281-
)}
223+
<div>
224+
<Checkbox
225+
disabled={protocol === 'https:'}
226+
checked={useTls}
227+
onChange={() => setUseTls((prev) => !prev)}>
228+
HTTPS(请确保开启 <code>http-api-tls</code>
229+
</Checkbox>
282230
</div>
231+
283232
<div tw="mt-6">
284233
<LoadingButton
285234
type="submit"

src/types.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,7 @@ export interface Profile {
135135
platform: 'macos' | 'ios'
136136
platformVersion: string
137137
platformBuild: number
138-
helperHost?: string
139-
helperPort?: number
138+
tls?: boolean
140139
}
141140

142141
export interface EvaluateResult {

src/utils/fetcher.ts

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios'
1+
import axios, { AxiosRequestConfig } from 'axios'
22
import { toast } from 'react-toastify'
3-
import set from 'lodash-es/set'
43

54
const client = axios.create({
65
baseURL: '/v1',
@@ -24,21 +23,15 @@ export function setServer(
2423
port: number,
2524
key: string,
2625
options?: {
27-
helperHost: string
28-
helperPort: number
26+
tls?: boolean
2927
},
3028
): void {
31-
const { protocol } = window.location
32-
client.defaults.headers['X-Key'] = key
29+
const useTls = options?.tls === true
30+
31+
client.defaults.headers['x-key'] = key
3332
client.defaults.timeout = 5000
3433

35-
if (options) {
36-
client.defaults.baseURL = `https://${options.helperHost}:${options.helperPort}/v1`
37-
client.defaults.headers['x-surge-host'] = host
38-
client.defaults.headers['x-surge-port'] = port
39-
} else {
40-
client.defaults.baseURL = `${protocol}//${host}:${port}/v1`
41-
}
34+
client.defaults.baseURL = `${useTls ? 'https:' : 'http:'}//${host}:${port}/v1`
4235
}
4336

4437
const fetcher = <T>(requestConfig: AxiosRequestConfig): Promise<T> => {
@@ -70,26 +63,4 @@ const fetcher = <T>(requestConfig: AxiosRequestConfig): Promise<T> => {
7063
})
7164
}
7265

73-
export const bareFetcher = <T>(
74-
requestConfig: AxiosRequestConfig & { url: string },
75-
options?: {
76-
helperHost: string
77-
helperPort: number
78-
},
79-
): Promise<AxiosResponse<T>> => {
80-
if (options) {
81-
const url = new URL(requestConfig.url)
82-
set(requestConfig, 'headers["x-surge-host"]', url.hostname)
83-
set(requestConfig, 'headers["x-surge-port"]', url.port)
84-
85-
url.hostname = options.helperHost
86-
url.port = `${options.helperPort}`
87-
url.protocol = 'https:'
88-
89-
requestConfig.url = url.toString()
90-
}
91-
92-
return axios.request<T>(requestConfig)
93-
}
94-
9566
export default fetcher

0 commit comments

Comments
 (0)