-
Notifications
You must be signed in to change notification settings - Fork 876
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #466 from Meituan-Dianping/refactor/server_decoder
后端日志解析优化,解决日志丢失问题
- Loading branch information
Showing
4 changed files
with
264 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include "base_util.h" | ||
#include "clogan_core.h" | ||
#include <stdio.h> | ||
#include <time.h> | ||
|
||
int main(int argc, const char **argv) { | ||
char key[16] = "0123456789012345"; | ||
char iv[16] = "0123456789012345"; | ||
int code; | ||
|
||
clogan_debug(0); | ||
code = clogan_init("log", "log", 10 * 1024 * 1024, key, iv); | ||
printf("[init]: %d\n", code); | ||
|
||
code = clogan_open("logan.bin"); | ||
printf("[open]: %d\n", code); | ||
|
||
long long ts = get_system_current_clogan(); | ||
time_t now = time(NULL); | ||
char *now_str = ctime(&now); | ||
printf("[now]: %s\n", now_str); | ||
|
||
clogan_flush(); | ||
clogan_write(10, now_str, ts, "main", 1, 1); | ||
clogan_write(10, "[log 1]", ts++, "main", 1, 1); | ||
clogan_write(10, "[log 2]", ts++, "main", 1, 1); | ||
clogan_write(10, "[log 3]", ts++, "main", 1, 1); | ||
clogan_write(10, "[log 4]", ts++, "main", 1, 1); | ||
clogan_write(10, "[log 5]", ts++, "main", 1, 1); | ||
clogan_write(10, "how", ts++, "main", 1, 1); | ||
clogan_write(10, "are", ts++, "main", 1, 1); | ||
clogan_write(10, "you", ts++, "main", 1, 1); | ||
clogan_write(10, "fine", ts++, "main", 1, 1); | ||
// clogan_flush(); | ||
|
||
printf("[exit]\n"); | ||
} |
136 changes: 136 additions & 0 deletions
136
Logan/Server/src/main/java/com/meituan/logan/web/parser/LegacyLoganProtocol.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package com.meituan.logan.web.parser; | ||
|
||
import com.meituan.logan.web.enums.ResultEnum; | ||
import com.meituan.logan.web.model.Tuple; | ||
import org.apache.commons.io.IOUtils; | ||
import org.apache.log4j.Logger; | ||
import org.bouncycastle.jce.provider.BouncyCastleProvider; | ||
import org.springframework.core.io.support.PropertiesLoaderUtils; | ||
|
||
import javax.annotation.PreDestroy; | ||
import javax.crypto.Cipher; | ||
import javax.crypto.spec.IvParameterSpec; | ||
import javax.crypto.spec.SecretKeySpec; | ||
import java.io.*; | ||
import java.nio.ByteBuffer; | ||
import java.security.Security; | ||
import java.util.Properties; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.zip.GZIPInputStream; | ||
|
||
/** | ||
* 老版本解析器 | ||
*/ | ||
public class LegacyLoganProtocol { | ||
|
||
private static final Logger LOGGER = Logger.getLogger(LoganProtocol.class); | ||
|
||
private static final char ENCRYPT_CONTENT_START = '\1'; | ||
|
||
private static final String AES_ALGORITHM_TYPE = "AES/CBC/NoPadding"; | ||
|
||
private static AtomicBoolean initialized = new AtomicBoolean(false); | ||
|
||
static { | ||
initialize(); | ||
} | ||
|
||
private ByteBuffer wrap; | ||
private FileOutputStream fileOutputStream; | ||
|
||
public LegacyLoganProtocol(InputStream stream, File file) { | ||
try { | ||
wrap = ByteBuffer.wrap(IOUtils.toByteArray(stream)); | ||
fileOutputStream = new FileOutputStream(file); | ||
} catch (IOException e) { | ||
LOGGER.error(e); | ||
} | ||
} | ||
|
||
public ResultEnum process() { | ||
while (wrap.hasRemaining()) { | ||
while (wrap.hasRemaining() && wrap.get() == ENCRYPT_CONTENT_START) { | ||
byte[] encrypt = new byte[wrap.getInt()]; | ||
if (!tryGetEncryptContent(encrypt) || !decryptAndAppendFile(encrypt)) { | ||
return ResultEnum.ERROR_DECRYPT; | ||
} | ||
} | ||
} | ||
return ResultEnum.SUCCESS; | ||
} | ||
|
||
private boolean tryGetEncryptContent(byte[] encrypt) { | ||
try { | ||
wrap.get(encrypt); | ||
} catch (java.nio.BufferUnderflowException e) { | ||
LOGGER.error(e); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
private boolean decryptAndAppendFile(byte[] encrypt) { | ||
boolean result = false; | ||
try { | ||
Cipher aesEncryptCipher = Cipher.getInstance(AES_ALGORITHM_TYPE); | ||
Tuple<String, String> secureParam = getSecureParam(); | ||
if (secureParam == null) { | ||
return false; | ||
} | ||
SecretKeySpec secretKeySpec = new SecretKeySpec(secureParam.getFirst().getBytes(), "AES"); | ||
aesEncryptCipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(secureParam.getSecond().getBytes())); | ||
byte[] compressed = aesEncryptCipher.doFinal(encrypt); | ||
byte[] plainText = decompress(compressed); | ||
result = true; | ||
fileOutputStream.write(plainText); | ||
fileOutputStream.flush(); | ||
} catch (Exception e) { | ||
LOGGER.error(e); | ||
} | ||
return result; | ||
} | ||
|
||
private static byte[] decompress(byte[] contentBytes) { | ||
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) { | ||
IOUtils.copy(new GZIPInputStream(new ByteArrayInputStream(contentBytes)), out); | ||
return out.toByteArray(); | ||
} catch (IOException e) { | ||
LOGGER.error(e); | ||
} | ||
return new byte[0]; | ||
} | ||
|
||
@PreDestroy | ||
public void closeFileSteam() { | ||
try { | ||
fileOutputStream.close(); | ||
} catch (IOException e) { | ||
LOGGER.error(e); | ||
} | ||
} | ||
|
||
/** | ||
* BouncyCastle作为安全提供,防止我们加密解密时候因为jdk内置的不支持改模式运行报错。 | ||
**/ | ||
private static void initialize() { | ||
if (initialized.get()) { | ||
return; | ||
} | ||
Security.addProvider(new BouncyCastleProvider()); | ||
initialized.set(true); | ||
} | ||
|
||
|
||
private static Tuple<String, String> getSecureParam() { | ||
try { | ||
Properties properties = PropertiesLoaderUtils.loadAllProperties("secure.properties"); | ||
Tuple<String, String> tuple = new Tuple<>(); | ||
tuple.setFirst(properties.getProperty("AES_KEY")); | ||
tuple.setSecond(properties.getProperty("IV")); | ||
return tuple; | ||
} catch (IOException e) { | ||
LOGGER.error(e); | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters