This repository has been archived by the owner on Jul 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 233
/
Main.kt
191 lines (157 loc) · 6.14 KB
/
Main.kt
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
186
187
188
189
190
191
/**
* Pokemon Go Bot Copyright (C) 2016 PokemonGoBot-authors (see authors.md for more information)
* This program comes with ABSOLUTELY NO WARRANTY;
* This is free software, and you are welcome to redistribute it under certain conditions.
*
* For more information, refer to the LICENSE file in this repositories root directory
*/
package ink.abb.pogo.scraper
import com.google.common.util.concurrent.AtomicDouble
import ink.abb.pogo.api.PoGoApiImpl
import ink.abb.pogo.api.auth.CredentialProvider
import ink.abb.pogo.api.auth.GoogleAutoCredentialProvider
import ink.abb.pogo.api.auth.PtcCredentialProvider
import ink.abb.pogo.api.util.SystemTimeImpl
import ink.abb.pogo.scraper.services.BotService
import ink.abb.pogo.scraper.util.Log
import ink.abb.pogo.scraper.util.credentials.GoogleAutoCredentials
import ink.abb.pogo.scraper.util.credentials.GoogleCredentials
import ink.abb.pogo.scraper.util.credentials.PtcCredentials
import ink.abb.pogo.scraper.util.directions.getAltitude
import okhttp3.Credentials
import okhttp3.OkHttpClient
import org.springframework.boot.SpringApplication
import java.io.File
import java.io.FileInputStream
import java.io.FileNotFoundException
import java.net.InetSocketAddress
import java.net.Proxy
import java.nio.file.Paths
import java.util.*
import java.util.logging.LogManager
import javax.swing.text.rtf.RTFEditorKit
val time = SystemTimeImpl()
fun getAuth(settings: Settings, http: OkHttpClient): CredentialProvider {
val credentials = settings.credentials
val auth = if (credentials is GoogleCredentials) {
Log.red("Google User Credential Provider is deprecated; Use google-auto")
System.exit(1)
null
} else if (credentials is GoogleAutoCredentials) {
GoogleAutoCredentialProvider(http, credentials.username, credentials.password, time)
} else if (credentials is PtcCredentials) {
PtcCredentialProvider(http, credentials.username, credentials.password, time)
} else {
throw IllegalStateException("Unknown credentials: ${credentials.javaClass}")
}
return auth!!
}
fun main(args: Array<String>) {
LogManager.getLogManager().reset()
SpringApplication.run(PokemonGoBotApplication::class.java, *args)
}
fun loadProperties(filename: String): Properties {
val properties = Properties()
Log.green("Trying to read ${Paths.get(filename).toAbsolutePath()}")
var failed = false
try {
FileInputStream(filename).use {
try {
properties.load(it)
} catch (e: Exception) {
failed = true
}
}
} catch (e: FileNotFoundException) {
throw e
}
if (failed) {
FileInputStream(filename).use {
val rtfParser = RTFEditorKit()
val document = rtfParser.createDefaultDocument()
rtfParser.read(it.reader(), document, 0)
val text = document.getText(0, document.length)
properties.load(text.byteInputStream())
Log.red("Config file encoded as Rich Text Format (RTF)!")
}
}
return properties
}
fun startDefaultBot(http: OkHttpClient, service: BotService) {
var properties: Properties? = null
val attemptFilenames = arrayOf("config.properties", "config.properties.txt", "config.properties.rtf")
val dir = File(System.getProperty("java.class.path")).absoluteFile.parentFile
var filename = ""
fileLoop@ for (path in arrayOf(Paths.get("").toAbsolutePath(), dir)) {
for (attemptFilename in attemptFilenames) {
try {
filename = attemptFilename
properties = loadProperties("${path.toString()}/$filename")
break@fileLoop
} catch (e: FileNotFoundException) {
Log.red("$filename file not found")
}
}
}
if (properties == null) {
Log.red("No config files found. Exiting.")
System.exit(1)
return
} else {
val settings = SettingsParser(properties).createSettingsFromProperties()
service.addBot(startBot(settings, http))
}
}
fun startBot(settings: Settings, http: OkHttpClient): Bot {
var proxyHttp: OkHttpClient? = null
if (!settings.proxyServer.equals("") && settings.proxyPort > 0) {
Log.normal("Setting up proxy server for bot ${settings.name}: ${settings.proxyServer}:${settings.proxyPort}")
val proxyType: Proxy.Type
if (settings.proxyType.equals("HTTP"))
proxyType = Proxy.Type.HTTP
else if (settings.proxyType.equals("SOCKS"))
proxyType = Proxy.Type.SOCKS
else
proxyType = Proxy.Type.DIRECT
proxyHttp = http.newBuilder()
.proxy(Proxy(proxyType, InetSocketAddress(settings.proxyServer, settings.proxyPort)))
.proxyAuthenticator { route, response ->
response.request().newBuilder()
.header("Proxy-Authorization", Credentials.basic(settings.proxyUsername, settings.proxyPassword))
.build()
}
.build()
}
Log.normal("Logging in to game server...")
val auth =
if (proxyHttp == null) {
getAuth(settings, http)
} else {
getAuth(settings, proxyHttp)
}
val api =
if (proxyHttp == null)
PoGoApiImpl(http, auth, time)
else
PoGoApiImpl(proxyHttp, auth, time)
val lat = AtomicDouble(settings.latitude)
val lng = AtomicDouble(settings.longitude)
if (settings.saveLocationOnShutdown && settings.savedLatitude != 0.0 && settings.savedLongitude != 0.0) {
lat.set(settings.savedLatitude)
lng.set(settings.savedLongitude)
Log.normal("Loaded last saved location (${settings.savedLatitude}, ${settings.savedLongitude})")
}
api.setLocation(lat.get(), lng.get(), 0.0)
api.start()
Log.normal("Logged in successfully")
print("Getting profile data from pogo server")
while (!api.initialized) {
print(".")
Thread.sleep(1000)
}
println(".")
Thread.sleep(1000)
val bot = Bot(api, settings)
bot.start()
return bot
}