-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAMaasClient.java
More file actions
758 lines (687 loc) · 33.3 KB
/
Copy pathAMaasClient.java
File metadata and controls
758 lines (687 loc) · 33.3 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
package com.trend.cloudone.amaas;
import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.net.ssl.SSLException;
import io.grpc.ManagedChannel;
import io.grpc.netty.GrpcSslContexts;
import io.grpc.netty.NettyChannelBuilder;
import io.grpc.Status;
import io.grpc.stub.StreamObserver;
import io.grpc.stub.CallStreamObserver;
import io.grpc.StatusRuntimeException;
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
import io.netty.handler.ssl.SslContext;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.channel.ChannelFactory;
import io.netty.handler.proxy.Socks5ProxyHandler;
import com.google.protobuf.ByteString;
import com.trend.cloudone.amaas.scan.ScanGrpc;
import com.trend.cloudone.amaas.scan.ScanOuterClass;
import com.trend.cloudone.amaas.scan.ScanOuterClass.Stage;
/**
* AMaaS Client connecting to AMaaS gRPC server to provide API for malware scanning services. User can use the API
* to scan a file or a byte buffer.
*/
public final class AMaasClient {
private static final Logger logger = Logger.getLogger(AMaasClient.class.getName());
private static final long DEFAULT_SCAN_TIMEOUT = 300;
private static final int MAX_NUM_OF_TAGS = 8;
private static final int MAX_LENGTH_OF_TAG = 63;
private ManagedChannel channel;
private ScanGrpc.ScanStub asyncStub;
private AMaasCallCredentials cred;
private long timeoutSecs = DEFAULT_SCAN_TIMEOUT; // 3 minutes
private boolean bulk = true;
private EventLoopGroup eventLoopGroup;
/**
* AMaaSClient constructor. The enabledTLS is default to true and the appName is default to V1FS.
* @param region region you obtained your api key
* @param apiKey api key to be used
* @param timeoutInSecs number in seconds to wait for a scan. 0 default to 180 seconds.
* @throws AMaasException if an exception is detected, it will convert to AMassException.
*
*/
public AMaasClient(final String region, final String apiKey, final long timeoutInSecs) throws AMaasException {
this(region, null, apiKey, timeoutInSecs, true, null);
}
/**
* AMaaSClient constructor.
* @deprecated
* @param region region we obtained your api key
* @param apiKey api key to be used
* @param timeoutInSecs number in seconds to wait for a scan. 0 default to 180 seconds.
* @param enabledTLS boolean flag to enable or disable TLS
* @param appName application name to use.
* @throws AMaasException if an exception is detected, it will convert to AMassException.
*
*/
@Deprecated
public AMaasClient(final String region, final String apiKey, final long timeoutInSecs, final boolean enabledTLS, final String appName) throws AMaasException {
this(region, null, apiKey, timeoutInSecs, enabledTLS, null);
}
/**
* AMaaSClient constructor.
* @param region region we obtained your api key. If host is given, region is ignored.
* @param host AMaas scanner host address. Null if to use Trend AMaaS service specified in region.
* @param apiKey api key to be used
* @param timeoutInSecs number in seconds to wait for a scan. 0 default to 180 seconds.
* @param enabledTLS boolean flag to enable or disable TLS
* @param caCertPath File path of the CA certificate for hosted AMaaS Scanner server. null if using Trend AMaaS service.
* @throws AMaasException if an exception is detected, it will convert to AMassException.
*
*/
public AMaasClient(final String region, final String host, final String apiKey, final long timeoutInSecs, final boolean enabledTLS, final String caCertPath) throws AMaasException {
String target = this.identifyHostAddr(region, host);
if (target == null || target == "") {
throw new AMaasException(AMaasErrorCode.MSG_ID_ERR_INVALID_REGION, region, AMaasRegion.getAllRegionsAsString());
}
// Initialize proxy configuration
ProxyConfig proxyConfig = new ProxyConfig();
if (enabledTLS) {
log(Level.FINE, "Using prod grpc service {0}", target);
String verifyCertEnv = System.getenv("TM_AM_DISABLE_CERT_VERIFY");
boolean verifyCert = !("1".equals(verifyCertEnv));
SslContext context;
try {
if (!verifyCert) {
// Bypassing certificate verification
log(Level.FINE, "Bypassing certificate verification");
context = GrpcSslContexts.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
} else {
if (caCertPath != null && !caCertPath.isEmpty()) {
// Bring Your Own Certificate case
log(Level.FINE, "Using certificate {0}", caCertPath);
File certFile = Paths.get(caCertPath).toFile();
context = GrpcSslContexts.forClient().trustManager(certFile).build();
} else {
// Default SSL credentials case
log(Level.FINE, "Using default certificate");
context = GrpcSslContexts.forClient().build();
}
}
} catch (SSLException | UnsupportedOperationException e) {
throw new AMaasException(AMaasErrorCode.MSG_ID_ERR_LOAD_SSL_CERT);
}
this.channel = buildChannelWithProxy(target, proxyConfig, true, context);
} else {
log(Level.FINE, "Using grpc service with TLS disenabled {0}", target);
this.channel = buildChannelWithProxy(target, proxyConfig, false, null);
}
if (apiKey != null) {
this.cred = new AMaasCallCredentials(apiKey, AMaasConstants.V1FS_APP);
} else {
throw new AMaasException(AMaasErrorCode.MSG_ID_ERR_MISSING_AUTH);
}
if (timeoutInSecs > 0) {
this.timeoutSecs = timeoutInSecs;
}
this.asyncStub = ScanGrpc.newStub(this.channel).withCallCredentials(this.cred);
}
/**
* Close the client and release all resources.
* This method should be called when the client is no longer needed.
*/
public void close() {
try {
if (this.channel != null) {
this.channel.shutdownNow().awaitTermination(1, TimeUnit.SECONDS);
}
if (this.eventLoopGroup != null) {
this.eventLoopGroup.shutdownGracefully().awaitUninterruptibly(1, TimeUnit.SECONDS);
}
} catch (InterruptedException e) {
log(Level.WARNING, "Closing AMaaSClient throws exception: {0}", e.getMessage());
Thread.currentThread().interrupt();
}
}
@Override
protected void finalize() {
close();
}
private String identifyHostAddr(final String region, final String host) {
if (host != null && !host.isEmpty()) {
return host;
}
return AMaasRegion.getServiceFqdn(region);
}
/**
* Builds a ManagedChannel with proxy support if configured.
* @param target the target server address
* @param proxyConfig the proxy configuration
* @param enableTLS whether to enable TLS
* @param sslContext the SSL context for TLS connections
* @return configured ManagedChannel
*/
private ManagedChannel buildChannelWithProxy(final String target, final ProxyConfig proxyConfig, final boolean enableTLS, final SslContext sslContext) {
NettyChannelBuilder builder = NettyChannelBuilder.forTarget(target);
// Configure TLS
if (enableTLS && sslContext != null) {
builder.sslContext(sslContext);
} else if (!enableTLS) {
builder.usePlaintext();
}
final int keepAliveTimeSecs = 60;
final int keepAliveTimeoutSecs = 5;
final int connectTimeoutMs = 30000;
builder
.keepAliveTime(keepAliveTimeSecs, TimeUnit.SECONDS)
.keepAliveTimeout(keepAliveTimeoutSecs, TimeUnit.SECONDS)
.keepAliveWithoutCalls(true) // Keep connection alive even when idle
.withOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeoutMs);
log(
Level.FINE,
"Configured keep-alive: time={0}s, timeout={1}s, connectTimeout={2}ms",
keepAliveTimeSecs,
keepAliveTimeoutSecs,
connectTimeoutMs
);
// Configure proxy if needed
String proxyUrl = proxyConfig.getProxyUrl(target);
if (proxyUrl != null) {
log(Level.FINE, "Using proxy: {0} for target: {1}", proxyUrl, target);
configureProxy(builder, proxyConfig, proxyUrl);
} else {
log(Level.FINE, "No proxy configured for target: {0}", target);
}
return builder.build();
}
/**
* Configures proxy settings using Netty proxy handlers for SOCKS5 and Java system properties for HTTP.
* @param builder the NettyChannelBuilder to configure
* @param proxyConfig the proxy configuration
* @param proxyUrl the proxy URL to use
*/
private void configureProxy(final NettyChannelBuilder builder, final ProxyConfig proxyConfig, final String proxyUrl) {
InetSocketAddress proxyAddress = proxyConfig.getProxyAddress(proxyUrl);
if (proxyAddress == null) {
log(Level.WARNING, "Failed to parse proxy address: {0}", proxyUrl);
return;
}
if (proxyConfig.isSocks5Proxy(proxyUrl)) {
// Configure SOCKS5 proxy using Netty handlers
log(Level.FINE, "Configuring SOCKS5 proxy: {0}:{1}", proxyAddress.getHostName(), proxyAddress.getPort());
// Create EventLoopGroup for the channel
this.eventLoopGroup = new NioEventLoopGroup(1);
// Create custom channel factory with SOCKS5 proxy handler
ChannelFactory<NioSocketChannel> channelFactory = new ChannelFactory<NioSocketChannel>() {
@Override
public NioSocketChannel newChannel() {
NioSocketChannel channel = new NioSocketChannel();
// Add SOCKS5 proxy handler at the beginning of the pipeline after channel is active
channel.pipeline().addFirst("socks5-proxy", proxyConfig.hasProxyAuth()
? new Socks5ProxyHandler(proxyAddress, proxyConfig.getProxyUser(), proxyConfig.getProxyPass())
: new Socks5ProxyHandler(proxyAddress));
return channel;
}
};
// Configure the builder with custom settings for SOCKS5
builder.channelType(NioSocketChannel.class)
.eventLoopGroup(this.eventLoopGroup)
.channelFactory(channelFactory);
} else {
// Configure HTTP proxy using Java system properties (existing method)
log(Level.FINE, "Configuring HTTP proxy: {0}:{1}", proxyAddress.getHostName(), proxyAddress.getPort());
System.setProperty("http.proxyHost", proxyAddress.getHostName());
System.setProperty("http.proxyPort", String.valueOf(proxyAddress.getPort()));
System.setProperty("https.proxyHost", proxyAddress.getHostName());
System.setProperty("https.proxyPort", String.valueOf(proxyAddress.getPort()));
// For HTTP proxy with authentication, set authenticator
if (proxyConfig.hasProxyAuth()) {
log(Level.FINE, "Setting HTTP proxy authentication");
setProxyAuthenticator(proxyConfig);
}
}
}
/**
* Sets the proxy authenticator for both HTTP and SOCKS5 proxies.
* @param proxyConfig the proxy configuration with authentication details
*/
private void setProxyAuthenticator(final ProxyConfig proxyConfig) {
java.net.Authenticator.setDefault(new java.net.Authenticator() {
@Override
protected java.net.PasswordAuthentication getPasswordAuthentication() {
if (getRequestorType() == RequestorType.PROXY) {
return new java.net.PasswordAuthentication(
proxyConfig.getProxyUser(),
proxyConfig.getProxyPass().toCharArray()
);
}
return null;
}
});
}
private static void log(final Level level, final String msg, final Object... params) {
logger.log(level, msg, params);
}
/**
* Class to handle the bidirection request and response messages on the gRPC channel between the SDK client and the scan server.
* It implements the callback handlers of the response StreamObserver.
*/
static class AMaasServerCallback implements StreamObserver<ScanOuterClass.S2C> {
private static final int POLL_TIME_MILLIS = 200;
static final int DEFAULT_HEARTBEAT_INTERVAL_MS = 30 * 1000;
private StreamObserver<ScanOuterClass.C2S> requestObserver;
private AMaasReader reader;
private CountDownLatch finishCond = new CountDownLatch(1);
private Boolean done = false;
private String scanResult = null;
private Status.Code grpcStatus = Status.Code.UNKNOWN;
private int fetchCount = 0;
private long fetchSize = 0;
private boolean bulk = true;
private long start = System.currentTimeMillis();
private long timeoutSecs;
private final Object streamLock = new Object();
private volatile boolean streamClosed = false;
private ScheduledExecutorService heartbeatExecutor = null;
private int heartbeatIntervalMs = DEFAULT_HEARTBEAT_INTERVAL_MS;
AMaasServerCallback() {
}
void setHeartbeatIntervalMs(final int intervalMs) {
this.heartbeatIntervalMs = intervalMs;
}
protected void startHeartbeat() {
heartbeatExecutor = Executors.newSingleThreadScheduledExecutor(new java.util.concurrent.ThreadFactory() {
@Override
public Thread newThread(final Runnable r) {
Thread t = new Thread(r, "amaas-heartbeat");
t.setDaemon(true);
return t;
}
});
heartbeatExecutor.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
if (streamClosed) {
return;
}
ScanOuterClass.C2S hb = ScanOuterClass.C2S.newBuilder()
.setStage(Stage.STAGE_HEARTBEAT)
.build();
synchronized (streamLock) {
if (!streamClosed) {
try {
requestObserver.onNext(hb);
log(Level.FINE, "Sent heartbeat");
} catch (Exception e) {
log(Level.WARNING, "Heartbeat send failed: {0}", e.getMessage());
}
}
}
}
}, heartbeatIntervalMs, heartbeatIntervalMs, TimeUnit.MILLISECONDS);
}
protected void stopHeartbeat() {
if (heartbeatExecutor != null) {
heartbeatExecutor.shutdownNow();
heartbeatExecutor = null;
}
}
private AMaasException processError() {
AMaasException err = null;
if (this.grpcStatus == Status.Code.UNAUTHENTICATED) {
err = new AMaasException(AMaasErrorCode.MSG_ID_ERR_KEY_AUTH_FAILED);
} else {
err = new AMaasException(AMaasErrorCode.MSG_ID_GRPC_ERROR, this.grpcStatus.value(), this.grpcStatus.toString());
}
return err;
}
protected String waitTilExit() throws AMaasException {
try {
while (!this.done) {
this.finishCond.await();
}
} catch (InterruptedException err) {
throw new AMaasException(AMaasErrorCode.MSG_ID_ERR_UNEXPECTED_INTERRUPT);
}
if (this.grpcStatus == Status.Code.OK) {
return this.scanResult;
} else {
throw this.processError();
}
}
protected void setContext(final StreamObserver<ScanOuterClass.C2S> requestObserver, final AMaasReader reader, final boolean bulk, final long timeoutSecs) {
this.requestObserver = requestObserver;
this.reader = reader;
this.done = false;
this.bulk = bulk;
this.timeoutSecs = timeoutSecs;
}
@Override
public void onNext(final ScanOuterClass.S2C s2cMsg) {
log(Level.FINE, "Got message {0} at {1}", s2cMsg.getCmdValue(), s2cMsg.getBulkLengthCount());
final CallStreamObserver<ScanOuterClass.C2S> callObserver = (CallStreamObserver<ScanOuterClass.C2S>) requestObserver;
switch (s2cMsg.getCmd()) {
case CMD_RETR:
if (s2cMsg.getStage() != Stage.STAGE_RUN) {
log(Level.INFO, "Received unexpected command RETR at stage {0}", s2cMsg.getStage());
synchronized (streamLock) {
if (!streamClosed) {
streamClosed = true;
requestObserver.onError(new StatusRuntimeException(Status.ABORTED));
}
}
return;
}
java.util.List<java.lang.Integer> bulkLength;
java.util.List<java.lang.Integer> bulkOffset;
if (this.bulk) {
log(Level.FINE, "enter bulk mode");
int bulkCount = s2cMsg.getBulkLengthCount();
if (bulkCount > 1) {
log(Level.INFO, "bulk transfer triggered");
}
bulkLength = s2cMsg.getBulkLengthList();
bulkOffset = s2cMsg.getBulkOffsetList();
} else {
bulkLength = Arrays.asList(new Integer[]{s2cMsg.getLength()});
bulkOffset = Arrays.asList(new Integer[]{s2cMsg.getOffset()});
}
for (int i = 0; i < bulkLength.size(); i++) {
log(Level.INFO, "Bulk read length={0} at offset={1}", bulkLength.get(i).intValue(), bulkOffset.get(i).intValue());
byte[] bytes = new byte[bulkLength.get(i).intValue()];
try {
int rtnLength = reader.readBytes(bulkOffset.get(i).intValue(), bytes);
ByteString bytestr = ByteString.copyFrom(bytes);
this.fetchCount++;
this.fetchSize += rtnLength;
ScanOuterClass.C2S request = ScanOuterClass.C2S.newBuilder().setStage(Stage.STAGE_RUN).setChunk(bytestr).setOffset(bulkOffset.get(i).intValue()).build();
while (!callObserver.isReady()) {
try {
Thread.sleep(this.POLL_TIME_MILLIS);
log(Level.FINE, "stream is not ready yet, sleep {0}ms", this.POLL_TIME_MILLIS);
} catch (InterruptedException e) {
log(Level.INFO, "Receive interrupt during callObserver wait, reason: " + e.getMessage());
}
long duration = System.currentTimeMillis() - this.start;
if (TimeUnit.MILLISECONDS.toSeconds(duration) > this.timeoutSecs) {
log(Level.INFO, "DEADLINE_EXCEEDED {0}", duration);
synchronized (streamLock) {
if (!streamClosed) {
streamClosed = true;
requestObserver.onError(new StatusRuntimeException(Status.DEADLINE_EXCEEDED));
}
}
return;
}
}
synchronized (streamLock) {
if (!streamClosed) {
requestObserver.onNext(request);
}
}
} catch (IOException e) {
log(Level.SEVERE, "Exception when processing server message", e.getMessage());
synchronized (streamLock) {
if (!streamClosed) {
streamClosed = true;
requestObserver.onError(new StatusRuntimeException(Status.ABORTED));
}
}
}
}
break;
case CMD_QUIT:
this.scanResult = s2cMsg.getResult();
log(Level.INFO, "Scan succeeded: result={0} fetchCount={1} fetchSize={2}.", this.scanResult, this.fetchCount, this.fetchSize);
synchronized (streamLock) {
streamClosed = true;
requestObserver.onCompleted();
}
break;
default:
log(Level.WARNING, "Unknown command");
synchronized (streamLock) {
if (!streamClosed) {
streamClosed = true;
requestObserver.onError(new StatusRuntimeException(Status.INVALID_ARGUMENT));
}
}
}
}
@Override
public void onError(final Throwable t) {
synchronized (streamLock) {
streamClosed = true;
}
log(Level.WARNING, "scan Failed: {0}", Status.fromThrowable(t));
this.done = true;
this.grpcStatus = Status.fromThrowable(t).getCode();
this.finishCond.countDown();
}
@Override
public void onCompleted() {
synchronized (streamLock) {
streamClosed = true;
}
log(Level.INFO, "File successfully scanned.");
this.done = true;
this.grpcStatus = Status.Code.OK;
this.finishCond.countDown();
}
}
static AMaasException getTagListErrors(final String[] tagList) {
AMaasException except = null;
if (tagList.length > MAX_NUM_OF_TAGS) {
except = new AMaasException(AMaasErrorCode.MSG_ID_ERR_MAX_NUMBER_OF_TAGS, MAX_NUM_OF_TAGS);
}
for (String tag: tagList) {
if (tag == null || tag == "" || (tag.length() > MAX_LENGTH_OF_TAG)) {
except = new AMaasException(AMaasErrorCode.MSG_ID_ERR_LENGTH_OF_TAG, MAX_LENGTH_OF_TAG, tag);
break;
}
}
return except;
}
/**
* Scan a AMaasReader and return the scanned result.
*
* @param reader AMaasReader to be scanned.
* @param tagList List of tags.
* @param pml flag to indicate whether to use predictive machine learning detection.
* @param feedback flag to indicate whether to use Trend Micro Smart Protection Network's Smart Feedback.
* @param verbose flag to enable log verbose mode
* @param digest flag to enable calculation of digests for cache search and result lookup.
* @return String the scanned result in JSON format.
* @throws AMaasException if an exception is detected, it will convert to AMassException.
*/
@Deprecated
public String scanRun(final AMaasReader reader, final String[] tagList, final boolean pml, final boolean feedback, final boolean verbose, final boolean digest) throws AMaasException {
AMaasScanOptions options = AMaasScanOptions.builder()
.tagList(tagList)
.pml(pml)
.feedback(feedback)
.verbose(verbose)
.build();
return this.scanRun(reader, options);
}
/**
* Scan a AMaasReader and return the scanned result.
*
* @param reader AMaasReader to be scanned.
* @param options scan options containing configuration for the scan operation.
* @return String the scanned result in JSON format.
* @throws AMaasException if an exception is detected, it will convert to AMassException.
*/
public String scanRun(final AMaasReader reader, final AMaasScanOptions options) throws AMaasException {
boolean pml = options.isPml();
boolean feedback = options.isFeedback();
boolean verbose = options.isVerbose();
boolean activeContent = options.isActiveContent();
String[] tagList = options.getTagList();
long fileSize = reader.getLength();
AMaasServerCallback serverCallback = new AMaasServerCallback();
StreamObserver<ScanOuterClass.C2S> requestObserver = this.asyncStub.withDeadlineAfter(this.timeoutSecs, TimeUnit.SECONDS).run(serverCallback);
// initialize the callback context before proceeding
serverCallback.setContext(requestObserver, reader, this.bulk, this.timeoutSecs);
String sha1Str = reader.getHash(AMaasReader.HashType.HASH_SHA1);
String sha256Str = reader.getHash(AMaasReader.HashType.HASH_SHA256);
log(Level.FINE, "sha1={0} sha256={1}", sha1Str, sha256Str);
ScanOuterClass.C2S.Builder builder = ScanOuterClass.C2S
.newBuilder()
.setStage(Stage.STAGE_INIT)
.setFileName(reader.getIdentifier())
.setRsSize(fileSize)
.setOffset(0)
.setFileSha1(sha1Str)
.setFileSha256(sha256Str)
.setTrendx(pml)
.setSpnFeedback(feedback)
.setBulk(this.bulk)
.setVerbose(verbose)
.setActiveContent(activeContent);
if (tagList != null) {
AMaasException except = getTagListErrors(tagList);
if (except != null) {
throw except;
}
builder.addAllTags(Arrays.asList(tagList));
}
ScanOuterClass.C2S request = builder.build();
requestObserver.onNext(request);
serverCallback.startHeartbeat();
try {
return serverCallback.waitTilExit();
} finally {
serverCallback.stopHeartbeat();
}
}
/**
* Scan a file and return the scanned result.
*
* @param fileName Full path of a file to be scanned.
* @return String the scanned result in JSON format.
* @throws AMaasException if an exception is detected, it will convert to AMassException.
*/
@Deprecated
public String scanFile(final String fileName) throws AMaasException {
return this.scanFile(fileName, null, false, false, false, true);
}
/**
* Scan a file and return the scanned result.
*
* @deprecated
* @param fileName Full path of a file to be scanned.
* @param tagList List of tags.
* @param pml flag to indicate whether to enable predictive machine learning detection.
* @param feedback flag to indicate whether to use Trend Micro Smart Protection Network's Smart Feedback.
* @return String the scanned result in JSON format.
* @throws AMaasException if an exception is detected, it will convert to AMassException.
*/
@Deprecated
public String scanFile(final String fileName, final String[] tagList, final boolean pml, final boolean feedback) throws AMaasException {
return this.scanFile(fileName, tagList, pml, feedback, false, true);
}
/**
* Scan a file and return the scanned result.
*
* @param fileName Full path of a file to be scanned.
* @param tagList List of tags.
* @param pml flag to indicate whether to enable predictive machine learning detection.
* @param feedback flag to indicate whether to use Trend Micro Smart Protection Network's Smart Feedback.
* @param verbose flag to enable log verbose mode
* @param digest flag to enable calculation of digests for cache search and result lookup.
* @return String the scanned result in JSON format.
* @throws AMaasException if an exception is detected, it will convert to AMassException.
*/
@Deprecated
public String scanFile(final String fileName, final String[] tagList, final boolean pml, final boolean feedback, final boolean verbose, final boolean digest) throws AMaasException {
AMaasScanOptions options = AMaasScanOptions.builder()
.tagList(tagList)
.pml(pml)
.feedback(feedback)
.verbose(verbose)
.build();
return this.scanFile(fileName, digest, options);
}
/**
* Scan a file and return the scanned result.
*
* @param fileName Full path of a file to be scanned.
* @param digest flag to enable calculation of digests for cache search and result lookup.
* @param options scan options containing configuration for the scan operation.
* @return String the scanned result in JSON format.
* @throws AMaasException if an exception is detected, it will convert to AMassException.
*/
public String scanFile(final String fileName, final boolean digest, final AMaasScanOptions options) throws AMaasException {
AMaasFileReader fileReader = new AMaasFileReader(fileName, digest);
return this.scanRun(fileReader, options);
}
/**
* Scan a buffer and return the scanned result.
*
* @param buffer the buffer to be scanned.
* @param identifier A unique name to identify the buffer.
* @return String the scanned result in JSON format.
* @throws AMaasException if an exception is detected, it will convert to AMassException.
*/
@Deprecated
public String scanBuffer(final byte[] buffer, final String identifier) throws AMaasException {
return this.scanBuffer(buffer, identifier, null, false, false, false, true);
}
/**
* Scan a buffer and return the scanned result. (TBD: LSK remove this API).
*
* @deprecated
* @param buffer the buffer to be scanned.
* @param identifier A unique name to identify the buffer.
* @param tagList List of tags.
* @param pml flag to indicate whether to use predictive machine learning detection.
* @param feedback flag to indicate whether to use Trend Micro Smart Protection Network's Smart Feedback.
* @return String the scanned result in JSON format.
* @throws AMaasException if an exception is detected, it will convert to AMassException.
*/
@Deprecated
public String scanBuffer(final byte[] buffer, final String identifier, final String[] tagList, final boolean pml, final boolean feedback) throws AMaasException {
return this.scanBuffer(buffer, identifier, tagList, pml, feedback, false, true);
}
/**
* Scan a buffer and return the scanned result.
*
* @param buffer the buffer to be scanned.
* @param identifier A unique name to identify the buffer.
* @param tagList List of tags.
* @param pml flag to indicate whether to use predictive machine learning detection.
* @param feedback flag to indicate whether to use Trend Micro Smart Protection Network's Smart Feedback.
* @param verbose flag to enable log verbose mode
* @param digest flag to enable calculation of digests for cache search and result lookup.
* @return String the scanned result in JSON format.
* @throws AMaasException if an exception is detected, it will convert to AMassException.
*/
@Deprecated
public String scanBuffer(final byte[] buffer, final String identifier, final String[] tagList, final boolean pml, final boolean feedback, final boolean verbose, final boolean digest) throws AMaasException {
AMaasScanOptions options = AMaasScanOptions.builder()
.tagList(tagList)
.pml(pml)
.feedback(feedback)
.verbose(verbose)
.build();
return this.scanBuffer(buffer, identifier, digest, options);
}
/**
* Scan a buffer and return the scanned result.
*
* @param buffer the buffer to be scanned.
* @param identifier A unique name to identify the buffer.
* @param digest flag to enable calculation of digests for cache search and result lookup.
* @param options scan options containing configuration for the scan operation.
* @return String the scanned result in JSON format.
* @throws AMaasException if an exception is detected, it will convert to AMassException.
*/
public String scanBuffer(final byte[] buffer, final String identifier, final boolean digest, final AMaasScanOptions options) throws AMaasException {
AMaasBufferReader bufReader = new AMaasBufferReader(buffer, identifier, digest);
return this.scanRun(bufReader, options);
}
}