Skip to content

Commit

Permalink
fix: add log ip
Browse files Browse the repository at this point in the history
  • Loading branch information
storytellerF committed Dec 7, 2024
1 parent bd86cf8 commit e810159
Show file tree
Hide file tree
Showing 14 changed files with 62 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,28 @@ package com.storyteller_f.a.client_lib

import io.ktor.client.*
import io.ktor.client.engine.okhttp.*
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import java.util.concurrent.TimeUnit

actual fun getClient(block: HttpClientConfig<*>.() -> Unit): HttpClient {
val removeHeaderInterceptor = Interceptor { chain ->
val originalRequest = chain.request()

// 创建一个新的请求,移除特定头
val modifiedRequest = originalRequest.newBuilder()
.removeHeader("Accept-Charset") // 移除 Accept-Charset 头
.build()

// 继续请求链
chain.proceed(modifiedRequest)
}
return HttpClient(OkHttp) {
engine {
preconfigured = OkHttpClient.Builder()
.pingInterval(20, TimeUnit.SECONDS)
.build()
addInterceptor(removeHeaderInterceptor)
}
block()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
<base-config>
<trust-anchors>
<!-- Trust preinstalled CAs -->
<certificates src="system" />
<!-- Additionally trust user added CAs -->
<!-- <certificates src="user" />-->
</trust-anchors>
</base-config>
</network-security-config>
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ fun <T : Any> RefCellStateView(
}.padding(vertical = 8.dp),
contentAlignment = Alignment.Center
) {
Text(localState.e.message.toString(), modifier = Modifier.fillMaxHeight(), maxLines = 1)
val e = localState.e
Text(e.message.toString().take(100), modifier = Modifier.fillMaxHeight(), maxLines = 1)
}

else -> {
Expand Down
3 changes: 2 additions & 1 deletion deploy/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ services:
labels:
- "bunkerweb.INSTANCE=yes"
environment:
- SERVER_NAME=acommmunity.link
- SERVER_NAME=acommunity.link
- API_WHITELIST_IP=127.0.0.0/8 10.20.30.0/24
- USE_REVERSE_PROXY=yes
- REVERSE_PROXY_URL=/
Expand All @@ -66,6 +66,7 @@ services:
- EMAIL_LETS_ENCRYPT=34095089+storytellerF@users.noreply.github.com
- LETS_ENCRYPT_CLEAR_OLD_CERTS=yes
- DISABLE_DEFAULT_SERVER=no
- USE_LIMIT_REQ=no
networks:
- bw-universe
- bw-services
Expand Down
2 changes: 1 addition & 1 deletion dev.env
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ ELASTIC_NAME=elastic
ELASTIC_PASSWORD=elastic

MEDIA_SERVICE=minio
MINIO_URL=http://192.168.31.229:9000
MINIO_URL=http://localhost:9000
MINIO_PORT=9000
MINIO_PORT_CONSOLE=9001
MINIO_NAME=minioadmin
Expand Down
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ desugar_jdk_libs = "2.1.3"
detektVersion = "1.23.7"
emoji-reader = "2.0.6"
emojiKt = "2.0.1"
geoip2 = "4.2.1"
h2 = "2.2.224"
highlights = "1.0.0"
jimfs = "1.3.0"
Expand Down Expand Up @@ -61,6 +62,7 @@ kover = "0.7.4"
desugar_jdk_libs = { module = "com.android.tools:desugar_jdk_libs", version.ref = "desugar_jdk_libs" }
emoji-compose-m3 = { module = "org.kodein.emoji:emoji-compose-m3", version.ref = "emojiKt" }
emoji-kt = { module = "org.kodein.emoji:emoji-kt", version.ref = "emojiKt" }
geoip2 = { module = "com.maxmind.geoip2:geoip2", version.ref = "geoip2" }
highlights = { module = "dev.snipme:highlights", version.ref = "highlights" }
navigation-compose = { module = "org.jetbrains.androidx.navigation:navigation-compose", version.ref = "navigationCompose" }
pdfbox = { module = "org.apache.pdfbox:pdfbox", version.ref = "pdfbox" }
Expand Down
17 changes: 10 additions & 7 deletions scripts/service_scripts/start-service-on-remote.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,24 @@ else
echo "$FILE already exists. Skipping docker save."
fi

./scripts/tool_scripts/exec-until-success.sh ssh -i "$REMOTE_CERT_FILE" -p 422 "$PUSH_TO_REMOTE_URI" "mkdir -p a-server"

./scripts/tool_scripts/exec-until-success.sh ssh -i "$REMOTE_CERT_FILE" -p 422 "$PUSH_TO_REMOTE_URI" "mkdir -p a-server && mkdir -p /tmp/A"
sleep 2

md=$(md5sum "$FILE" | awk '{print $1}')
#mdRemote=$(ssh -i ~/Downloads/default.pem -p 422 ubuntu@54.153.231.70 "md5sum ./a-server/$FLAVOR.image.tar | awk '{print \$1}'")
#echo "local: $md remote: $mdRemote"
echo "put $FILE ./a-server/$FLAVOR.image.tar" | sftp -i "$REMOTE_CERT_FILE" -P 422 "$PUSH_TO_REMOTE_URI"

sleep 2

md=$(md5sum "$FILE" | awk '{print $1}')
ssh -i "$REMOTE_CERT_FILE" -p 422 "$PUSH_TO_REMOTE_URI" "echo ""$md" ".a-server//$FLAVOR.image.tar"" | md5sum -c -"

# 验证上传的文件完整性
ssh -i "$REMOTE_CERT_FILE" -p 422 "$PUSH_TO_REMOTE_URI" "echo ""$md" "./a-server/$FLAVOR.image.tar"" | md5sum -c -"
sleep 2

./scripts/tool_scripts/exec-until-success.sh ssh -i "$REMOTE_CERT_FILE" -p 422 "$PUSH_TO_REMOTE_URI" "sudo -s mv ./a-server/* /tmp/A"
./scripts/tool_scripts/exec-until-success.sh ssh -i "$REMOTE_CERT_FILE" -p 422 "$PUSH_TO_REMOTE_URI" "sudo cp ./a-server/$FLAVOR.image.tar /tmp/A/$FLAVOR.image.tar"
sleep 2

# 验证上传的文件完整性
ssh -i "$REMOTE_CERT_FILE" -p 422 "$PUSH_TO_REMOTE_URI" "echo ""$md" "/tmp/A/$FLAVOR.image.tar"" | md5sum -c -"
sleep 2

./scripts/tool_scripts/exec-until-success.sh ssh -i "$REMOTE_CERT_FILE" -p 422 "$PUSH_TO_REMOTE_URI" "$REMOTE_COMMAND"
2 changes: 1 addition & 1 deletion scripts/tool_scripts/exec-until-success.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ COMMAND="$@"

# 无限循环执行命令直到成功
while true; do
echo "Executing: $COMMAND"
#echo "Executing: $COMMAND"

# 执行命令
eval "$COMMAND"
Expand Down
1 change: 1 addition & 0 deletions server/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ dependencies {
implementation(libs.pdfbox)
implementation(libs.napier)
implementation(libs.emoji.reader.jvm)
implementation(libs.geoip2)
}

val isProd = project.findProperty("server.prod") == true
Expand Down
14 changes: 13 additions & 1 deletion server/src/main/kotlin/com/storyteller_f/a/server/Application.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.storyteller_f.a.server

import com.maxmind.geoip2.DatabaseReader
import com.perraco.utils.SnowflakeFactory
import com.storyteller_f.DatabaseFactory
import com.storyteller_f.a.server.auth.UserSession
Expand All @@ -17,6 +18,7 @@ import io.ktor.server.netty.*
import io.ktor.server.plugins.callid.*
import io.ktor.server.plugins.calllogging.*
import io.ktor.server.plugins.contentnegotiation.*
import io.ktor.server.plugins.origin
import io.ktor.server.plugins.ratelimit.RateLimit
import io.ktor.server.request.*
import io.ktor.server.resources.Resources
Expand All @@ -26,6 +28,8 @@ import kotlinx.coroutines.runBlocking
import kotlinx.serialization.json.Json
import org.slf4j.event.Level
import java.io.File
import java.net.InetAddress
import kotlin.jvm.optionals.getOrNull
import kotlin.system.exitProcess
import kotlin.time.Duration.Companion.seconds

Expand Down Expand Up @@ -91,6 +95,9 @@ private fun processPreSetData(map: Map<out Any, Any>) {
fun Application.module() {
val map = readEnv()
val backend = buildBackendFromEnv(map)
val reader = DatabaseReader.Builder(
ClassLoader.getSystemClassLoader().getResourceAsStream("GeoLite2-Country.mmdb")
).build()

install(ContentNegotiation) {
json()
Expand All @@ -100,7 +107,12 @@ fun Application.module() {
format { call ->
val status = call.response.status()
val httpMethod = call.request.httpMethod.value
"Status: $status, HTTP method: $httpMethod, Url: ${call.request.uri}"
val remoteAddress = call.request.origin.remoteAddress
val country = reader.tryCountry(InetAddress.getByName(remoteAddress)).getOrNull()
"""Status: $status, HTTP method: $httpMethod,
|Url: ${call.request.uri},
|Ip: $remoteAddress
|Region ${country?.toJson()}""".trimMargin()
}
}
install(CallId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ suspend inline fun <reified R : Any> RoutingContext.usePrincipalOrNull(block: (P
else -> call.respond(it)
}
}.onFailure {
if (it !is UnauthorizedException)
if (it !is UnauthorizedException) {
call.application.log.error("Occur exception", it)
}
respondError(it)
}
} catch (e: Exception) {
Expand Down
Binary file added server/src/main/resources/GeoLite2-Country.mmdb
Binary file not shown.
7 changes: 3 additions & 4 deletions shared/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import org.gradle.kotlin.dsl.implementation
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.targets.js.webpack.KotlinWebpackConfig

plugins {
Expand Down Expand Up @@ -48,7 +47,7 @@ kotlin {
sourceSets {
androidMain.dependencies {
implementation(libs.bcprov.jdk18on)
implementation(libs.bcpkix.jdk18on)
// implementation(libs.bcpkix.jdk18on)
implementation(projects.cryptoJvm)
// implementation(libs.bctls.jdk18on)
implementation(libs.cryptography.provider.jdk)
Expand All @@ -64,7 +63,7 @@ kotlin {
jvmMain.dependencies {
implementation(projects.cryptoJvm)
implementation(libs.bcprov.jdk18on)
implementation(libs.bcpkix.jdk18on)
// implementation(libs.bcpkix.jdk18on)
implementation(libs.cryptography.provider.jdk)
}
}
Expand Down

0 comments on commit e810159

Please sign in to comment.