Skip to content

Commit 88e59fb

Browse files
authored
Create backup ports for auth (#22)
* Create backup ports for auth * Add timeout if event cannot be given * Fix wrong assignment * Remove stupid timeout * Use requets instead of emits * Update workflow
1 parent 4a50b12 commit 88e59fb

File tree

14 files changed

+97
-31
lines changed

14 files changed

+97
-31
lines changed

.github/workflows/type-check.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
name: Type Check
22

3-
on:
4-
pull_request:
5-
types: [opened, reopened]
3+
on: push
64

75
jobs:
86
release:

src-tauri/Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src-tauri/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ ascii = "1.1.0"
2323
rodio = "0.16.0"
2424
tauri-plugin-store = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "dev" }
2525
tauri-plugin-autostart = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "dev" }
26+
port_scanner = "*"
2627

2728
[features]
2829
# by default Tauri runs in production mode

src-tauri/src/commands.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{fs::File, io::BufReader, sync::Mutex};
33
use rodio::{Decoder, OutputStream, Sink};
44
use tauri::{AppHandle, State, Window};
55

6-
use crate::server::AuthServer;
6+
use crate::{server::AuthServer, utils::get_available_socket_addr};
77

88
#[tauri::command]
99
pub fn play_notification_sound(app: AppHandle) {
@@ -38,7 +38,8 @@ pub fn set_icon_template(is_template: bool, app: AppHandle) {
3838
#[tauri::command]
3939
pub fn start_server(window: Window, state: State<'_, Mutex<AuthServer>>) {
4040
let mut server = state.lock().unwrap();
41-
server.listen(window);
41+
let addr = get_available_socket_addr();
42+
server.listen(window, addr);
4243
}
4344

4445
#[tauri::command]

src-tauri/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
mod commands;
77
mod server;
8+
mod utils;
89

910
use std::sync::Mutex;
1011

src-tauri/src/server.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use tauri::Window;
22

3-
use std::{io::Cursor, str::FromStr, sync::Arc};
3+
use std::{io::Cursor, net::SocketAddr, str::FromStr, sync::Arc};
44

55
use ::ascii::AsciiString;
66
use tiny_http::{Header, HeaderField, Method, Request, Response, Server};
@@ -74,7 +74,11 @@ fn set_content_type_html(response: &mut Response<Cursor<Vec<u8>>>) {
7474
}
7575

7676
fn handle_code_request(request: Request, window: &Window) {
77-
if *request.method() != Method::Get || !request.url().starts_with("/callback?code=") {
77+
let url = request.url();
78+
79+
if *request.method() != Method::Get
80+
|| (!request.url().starts_with("/callback?code=") && !request.url().starts_with("/ping"))
81+
{
7882
let mut response = Response::from_string(create_html(
7983
"Not Found - Gitification".to_owned(),
8084
"NOT FOUND".to_owned(),
@@ -86,7 +90,18 @@ fn handle_code_request(request: Request, window: &Window) {
8690
return;
8791
}
8892

89-
let url = request.url();
93+
if url.starts_with("/ping") {
94+
let mut response = Response::from_string("{\"pong\": true}");
95+
96+
response.add_header(Header {
97+
field: HeaderField::from_str("Content-Type").unwrap(),
98+
value: AsciiString::from_str("application/json").unwrap(),
99+
});
100+
101+
request.respond(response).unwrap();
102+
return;
103+
}
104+
90105
let code_query = url.split("?code=").collect::<Vec<&str>>()[1];
91106
let code = code_query.split("&").collect::<Vec<&str>>()[0];
92107

@@ -110,12 +125,12 @@ impl AuthServer {
110125
AuthServer { server: None }
111126
}
112127

113-
pub fn listen(&mut self, window: Window) {
128+
pub fn listen(&mut self, window: Window, addr: SocketAddr) {
114129
if self.server.is_some() {
115130
return;
116131
}
117132

118-
let server = Arc::new(Server::http("0.0.0.0:23846").unwrap());
133+
let server = Arc::new(Server::http(addr).unwrap());
119134
std::thread::spawn({
120135
let server = Arc::clone(&server);
121136
move || {
@@ -125,7 +140,7 @@ impl AuthServer {
125140
}
126141
});
127142

128-
self.server = Some(server)
143+
self.server = Some(server);
129144
}
130145

131146
pub fn stop(&mut self) {

src-tauri/src/utils.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use port_scanner::local_port_available;
2+
use std::net::SocketAddr;
3+
4+
pub const SERVER_PORTS: [u16; 3] = [23846, 15830, 12840];
5+
6+
pub fn get_available_socket_addr() -> SocketAddr {
7+
for port in SERVER_PORTS {
8+
if local_port_available(port) {
9+
return SocketAddr::from(([127, 0, 0, 1], port));
10+
}
11+
}
12+
13+
panic!("No available port found")
14+
}

src-tauri/tauri.conf.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
},
2323
"http": {
2424
"request": true,
25-
"scope": ["https://github.com/*", "https://api.github.com/*"]
25+
"scope": ["https://github.com/*", "https://api.github.com/*", "http://localhost:23846/*", "http://localhost:15830/*", "http://localhost:12840/*"]
2626
},
2727
"process": {
2828
"exit": true

src/api/app.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { ResponseType, fetch } from '@tauri-apps/api/http'
2+
import { SERVER_PORTS } from '../constants'
3+
4+
export async function getServerPort() {
5+
for (const port of SERVER_PORTS) {
6+
const res = await fetch<{ pong: true }>(`http://localhost:${port}/ping`, {
7+
method: 'GET',
8+
headers: { Accept: 'application/json' },
9+
responseType: ResponseType.JSON,
10+
})
11+
12+
if (res.ok && res.data.pong) {
13+
console.log({ res })
14+
return port
15+
}
16+
}
17+
18+
return SERVER_PORTS[0]
19+
}

src/api/constants.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,2 @@
1-
import { SERVER_PORT } from '../constants'
2-
import { createURL } from '../utils/url'
3-
41
export const GITHUB_AUTHORIZE_ENDPOINT = 'https://github.com/login/oauth/authorize'
52
export const GITHUB_AUTH_SCOPES = ['notifications', 'read:user']
6-
7-
export const GITHUB_AUTH_QUERIES = {
8-
client_id: import.meta.env.VITE_CLIENT_ID,
9-
scope: GITHUB_AUTH_SCOPES.join(' '),
10-
redirect_uri: `http://localhost:${SERVER_PORT}/callback`,
11-
}
12-
13-
export const GITHUB_AUTH_URL = createURL({ url: GITHUB_AUTHORIZE_ENDPOINT, query: GITHUB_AUTH_QUERIES })

0 commit comments

Comments
 (0)