aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 35df4a872452713300125e7b0b36bbd6f992dd5d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
mod consts;
mod jobs;
mod opcode;
mod player;

use ::futures::StreamExt;
use consts::{DEFAULT_SOCK_PATH, DEFAULT_TCP_URL};
use jobs::{process_decrypt_n_signature, process_fetch_update, GlobalState, JobOpcode};
use opcode::OpcodeDecoder;
use player::fetch_update;
use std::{env::args, sync::Arc};
use tokio::{
    fs::remove_file,
    io::{AsyncReadExt, AsyncWrite},
    net::{TcpListener, UnixListener},
    sync::Mutex,
};
use tokio_util::codec::Framed;

use crate::jobs::{
    process_decrypt_signature, process_get_signature_timestamp, process_player_status,
    process_player_update_timestamp,
};

macro_rules! loop_main {
    ($i:ident, $s:ident) => {
        println!("Fetching player");
        match fetch_update($s.clone()).await {
            Ok(()) => println!("Successfully fetched player"),
            Err(x) => {
                println!("Error occured while trying to fetch the player: {:?}", x);
            }
        }
        loop {
            let (socket, _addr) = $i.accept().await.unwrap();

            let cloned_state = $s.clone();
            tokio::spawn(async move {
                process_socket(cloned_state, socket).await;
            });
        }
    };
}
#[tokio::main]
async fn main() {
    let args: Vec<String> = args().collect();
    let socket_url: &str = match args.get(1) {
        Some(stringref) => stringref,
        None => DEFAULT_SOCK_PATH,
    };

    // have to please rust
    let state: Arc<GlobalState> = Arc::new(GlobalState::new());

    if socket_url == "--tcp" {
        let socket_tcp_url: &str = match args.get(2) {
            Some(stringref) => stringref,
            None => DEFAULT_TCP_URL,
        };
        let tcp_socket = match TcpListener::bind(socket_tcp_url).await {
            Ok(x) => x,
            Err(x) => {
                println!("Error occurred while trying to bind: {}", x);
                return;
            }
        };
        loop_main!(tcp_socket, state);
    } else if socket_url == "--test" {
        // TODO: test the API aswell, this only tests the player script extractor
        println!("Fetching player");
        match fetch_update(state.clone()).await {
            Ok(()) => std::process::exit(0),
            Err(_x) => std::process::exit(-1),
        }
    } else {
        let unix_socket = match UnixListener::bind(socket_url) {
            Ok(x) => x,
            Err(x) => {
                if x.kind() == std::io::ErrorKind::AddrInUse {
                    remove_file(socket_url).await;
                    UnixListener::bind(socket_url).unwrap()
                } else {
                    println!("Error occurred while trying to bind: {}", x);
                    return;
                }
            }
        };
        loop_main!(unix_socket, state);
    }
}

async fn process_socket<W>(state: Arc<GlobalState>, socket: W)
where
    W: AsyncReadExt + Send + AsyncWrite + 'static,
{
    let decoder = OpcodeDecoder {};
    let str = Framed::new(socket, decoder);

    let (sink, mut stream) = str.split();

    let arc_sink = Arc::new(Mutex::new(sink));
    while let Some(opcode_res) = stream.next().await {
        match opcode_res {
            Ok(opcode) => {
                //println!("Received job: {}", opcode.opcode);

                match opcode.opcode {
                    JobOpcode::ForceUpdate => {
                        let cloned_state = state.clone();
                        let cloned_sink = arc_sink.clone();
                        tokio::spawn(async move {
                            process_fetch_update(cloned_state, cloned_sink, opcode.request_id)
                                .await;
                        });
                    }
                    JobOpcode::DecryptNSignature => {
                        let cloned_state = state.clone();
                        let cloned_sink = arc_sink.clone();
                        tokio::spawn(async move {
                            process_decrypt_n_signature(
                                cloned_state,
                                opcode.signature,
                                cloned_sink,
                                opcode.request_id,
                            )
                            .await;
                        });
                    }
                    JobOpcode::DecryptSignature => {
                        let cloned_state = state.clone();
                        let cloned_sink = arc_sink.clone();
                        tokio::spawn(async move {
                            process_decrypt_signature(
                                cloned_state,
                                opcode.signature,
                                cloned_sink,
                                opcode.request_id,
                            )
                            .await;
                        });
                    }
                    JobOpcode::GetSignatureTimestamp => {
                        let cloned_state = state.clone();
                        let cloned_sink = arc_sink.clone();
                        tokio::spawn(async move {
                            process_get_signature_timestamp(
                                cloned_state,
                                cloned_sink,
                                opcode.request_id,
                            )
                            .await;
                        });
                    }
                    JobOpcode::PlayerStatus => {
                        let cloned_state = state.clone();
                        let cloned_sink = arc_sink.clone();
                        tokio::spawn(async move {
                            process_player_status(cloned_state, cloned_sink, opcode.request_id)
                                .await;
                        });
                    }
                    JobOpcode::PlayerUpdateTimestamp => {
                        let cloned_state = state.clone();
                        let cloned_sink = arc_sink.clone();
                        tokio::spawn(async move {
                            process_player_update_timestamp(
                                cloned_state,
                                cloned_sink,
                                opcode.request_id,
                            )
                            .await;
                        });
                    }
                    _ => {
                        continue;
                    }
                }
            }
            Err(x) => {
                println!("I/O error: {:?}", x);
                break;
            }
        }
    }
}