0%

1. 基于 TCP 客户端的处理过程

客户端和服务端建立连接后, 客户端将发送请求来协商版本和认证方法:

VER NMETHODS METHODS
1 1 1 ~ 255
  • VER 协议版本. Socks5 协议中, VER 被置为 X’05’
  • NMETHODS METHODS 中出现的认证方法的个数. 客户端提供多种认证方法供服务端选择.
  • METHOD 验证方法
    • X'00' 无验证需求
    • X'01' 通用安全服务应用程序接口(GSSAPI)
    • X'02' 用户名/密码(USERNAME/PASSWORD)
    • X'03' ~ X'7F' IANA 分配(IANA ASSIGNED)
    • X'80' ~ X'FE' 私人方法保留(RESERVED FOR PRIVATE METHODS)

服务端选择其中一种认证方法并返回:

VER METHOD
1 1

如果选择的认证方法值为 X'FF', 则表示没有接受任何一种由客户端给出的认证方法, 客户端必须关闭连接.

协商过程结束后, 客户端将发送详细的请求信息.
如果协商的方法中有以完整性检查和/或安全性为目的的封装, 这些请求必须按照该方法所定义的方式进行封装.

VER CMD RSV ATYP DST.ADDR DST.PROT
1 1 X’00’ 1 Variable 2
  • VER 协议版本: X’05’
  • CMD
    • CONNECT X’01’
    • BIND X’02’
    • UDP ASSOCIATE X’03’
  • RSV 保留 (RESERVED)
  • ATYP DST.ADDR 的地址类型 address type of following address
    • IP V4 address: X’01’, 4个字节
    • DOMAINNAME: X’03’, 字段的第一个字节是以字节为单位的该域名的长度, 之后是域名, 中间没有结束符 NUL (X’00’)
    • IP V6 address: X’04’, 16字节
  • DST.ADDR 目标地址 desired destination address
  • DST.PORT 目标端口 desired destination port in network octet order

服务端将根据请求, 以如下格式返回:

VER REP RSV ATYP BND.ADDR BND.PORT
1 1 X’00’ 1 Variable 2
  • VER 协议版本: X’05’
  • REP Reply field:
    • X'00' succeeded
    • X'01' general SOCKS server failure
    • X'02' connection not allowed by ruleset
    • X'03' Network unreachable
    • X'04' Host unreachable
    • X'05' Connection refused
    • X'06' TTL expired
    • X'07' Command not supported
    • X'08' Address type not supported
    • X'09' ~ X'FF' unassigned
  • RSV RESERVED 必须被置为 X’00’
  • ATYP address type of following address
    • IP V4 address: X'01'
    • DOMAINNAME: X'03'
    • IP V6 address: X'04'
  • BND.ADDR server bound address
  • BND.PORT server bound port in network octet order

2. Resource

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
# 声明数组
array=("val1" "val2" "val3")
# 声明键值对
declare -A map=([key1]="val1" [key2]="val2" [key3]="val3")

# 取数组所有键, 近似于 ${!map[*]}
for key in ${!map[@]};do
echo ${map[$key]}
done

# 取数组所有值, 近似于 ${array[*]}
for ele in ${!array[@]};do
echo $ele
done

# 取数组的长度, 等价于 ${#array[*]}
echo ${#array[@]}

# 去重
array=($(awk -vRS=' ' '!a[$1]++' <<< ${array[@]}))

# 数组拼接
array=($array1 $array2)

# 将字符串分割成数组
# 方法 1
OLD_IFS="$IFS"
IFS=","
arr=($a)
IFS="$OLD_IFS"
for s in ${arr[@]}
do
echo "$s"
done
# 方法 2
str="192.168.31.65"
array=(${str//\./ })
for i in "${!array[@]}"; do
echo "$i=>${array[i]}"
done

# 删除元素
# 删除某个元素
unset array[0]

# 删除整个数组
unset array

1. Resource

https://www.cnblogs.com/sco1234/p/8906527.html

1. Resource

exec [-cl] [-a name] [command [arguments]]

exec 命令被执行时, 将替换当前 shell 进程, 且 exec 命令之后的命令将不再执行.
例外: 当 exec 命令对文件描述符操作的时候, 就不会替换 shell, 而是操作完成后还会继续执行后面的命令. 如 exec 3<&0, 表示将操作符3也指向标准输入

类似的:
source. 命令执行时, 将不启用新的 shell, 设定的局部变量在执行完成后仍然有效
bashsh 命令执行时, 将另起一个子 shell, 并继承父 shell 的环境变量, 子 shell 的变量不影响父 shell 中的.

-c 使得命令 command 在一个空环境中执行
-l 将在传递给 command 的第 0 个参数前面加上一个连字符 (dash,`-’)
-a 会将 name 作为第 0 个参数传递给要执行的命令

1. 安装

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
# 安装
curl -L https://alibaba.github.io/arthas/install.sh | sh

# 卸载
## 相关 jar 包放在 ` ~/.arthas/` 下
rm -rf ~/.arthas/

# 启动
## 当前版本 3.0.4, 服务启动需要 JDK-8 | JDK-9 , 暂不支持 JDK-10
## ./as.sh -h 可以查看帮助文档
## 启动日志在 ~/logs/arthas/arthas.log
## 本地连接可直接输入命令
./as.sh
## 远程连接可使用
# PID: 目标 Java 进程 ID(请确保执行当前执行命令的用户必须有足够的权限操作对应的 Java 进程)
# IP: Arthas Server 侦听的地址, 默认值是 127.0.0.1. Arthas允许多个用户同时访问, 并且各自的命令不会相互干扰执行
# PORT: 目标服务器 Arthas Server 的端口号, 默认的端口号是 3658
./as.sh <PID>@[IP:PORT]

# 模糊搜索类
sc *$class_name

# 查看类中有哪些方法
sm $full_class_name

# 查看方法被调用的情况
# -x n 输出的对象结构深度
# -b 在方法调用之前观察
# -s 在方法返回之后观察
# -e 在方法异常之后观察
# -f 在方法结束之后(正常返回和异常返回)观察, 默认打开
watch $full_class_name $method_name "{params,target,returnObj}" -x 2 -b -s

# 追踪调用链路, 并统计执行时间
# -j 过滤掉jdk的函数
# '#cost > 10' 根据调用耗时过滤
trace -j $full_class_name $method_name '#cost > 10'

# 退出
## Attach到目标进程上的arthas还会继续运行, 端口会保持开放, 下次连接时可以直接连接上.
## exit 效果同 quit
quit
## 完全退出
## **如果没有完全退出, 下次登录会重新进入之前的进程, 而参数的<PID>不起作用**
shutdown

2. FAQ

2.1. Error during processing the command: null

出现该提示, 表示 java-agent 冲突了
已经存在其他使用 java-agent 配置了, 导致 arthas 无法 trace, watch.

如果要 arthas 正常使用, 必须禁止其他 java-agent 存在

3. Resource

1. Obtain Stream

Streams can be obtained in a number of ways. Some examples include:

  • From a Collection via the stream() and parallelStream() methods;
  • From an array via Arrays.stream(Object[]);
  • From static factory methods on the stream classes, such as Stream.of(Object[]), IntStream.range(int, int) or Stream.iterate(Object, UnaryOperator);
  • The lines of a file can be obtained from BufferedReader.lines();
  • Streams of file paths can be obtained from methods in Files;
  • Streams of random numbers can be obtained from Random.ints();
  • Numerous other stream-bearing methods in the JDK, including BitSet.stream(), Pattern.splitAsStream(java.lang.CharSequence), and JarFile.stream().

2. API

2.1. terminal operation

  • R collect​(Supplier supplier, BiConsumer<R,​? super T> accumulator, BiConsumer<R,​R> combiner) ??
    Performs a mutable reduction operation on the elements of this stream.

  • <R,​A> R collect​(Collector<? super T,​A,​R> collector)
    Performs a mutable reduction operation on the elements of this stream using a Collector.

2.1.1. short-circuiting terminal operation

  • boolean allMatch​(Predicate<? super T> predicate)
    Returns true if either all elements of the stream match the provided predicate or the stream is empty, otherwise false.

  • boolean anyMatch​(Predicate<? super T> predicate)
    Returns true if any elements of the stream match the provided predicate, otherwise false.

2.2. intermediate operation

static Stream concat​(Stream<? extends T> a, Stream<? extends T> b)
Creates a lazily concatenated stream whose elements are all the elements of the first stream followed by all the elements of the second stream.
long count()
Returns the count of elements in this stream.
Stream distinct()
Returns a stream consisting of the distinct elements (according to Object.equals(Object)) of this stream.
default Stream dropWhile​(Predicate<? super T> predicate)
Returns, if this stream is ordered, a stream consisting of the remaining elements of this stream after dropping the longest prefix of elements that match the given predicate.
static Stream empty()
Returns an empty sequential Stream.
Stream filter​(Predicate<? super T> predicate)
Returns a stream consisting of the elements of this stream that match the given predicate.
Optional findAny()
Returns an Optional describing some element of the stream, or an empty Optional if the stream is empty.
Optional findFirst()
Returns an Optional describing the first element of this stream, or an empty Optional if the stream is empty.
Stream flatMap​(Function<? super T,​? extends Stream<? extends R>> mapper)
Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element.
DoubleStream flatMapToDouble​(Function<? super T,​? extends DoubleStream> mapper)
Returns an DoubleStream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element.
IntStream flatMapToInt​(Function<? super T,​? extends IntStream> mapper)
Returns an IntStream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element.
LongStream flatMapToLong​(Function<? super T,​? extends LongStream> mapper)
Returns an LongStream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element.
void forEach​(Consumer<? super T> action)
Performs an action for each element of this stream.
void forEachOrdered​(Consumer<? super T> action)
Performs an action for each element of this stream, in the encounter order of the stream if the stream has a defined encounter order.
static Stream generate​(Supplier<? extends T> s)
Returns an infinite sequential unordered stream where each element is generated by the provided Supplier.
static Stream iterate​(T seed, Predicate<? super T> hasNext, UnaryOperator next)
Returns a sequential ordered Stream produced by iterative application of the given next function to an initial element, conditioned on satisfying the given hasNext predicate.
static Stream iterate​(T seed, UnaryOperator f)
Returns an infinite sequential ordered Stream produced by iterative application of a function f to an initial element seed, producing a Stream consisting of seed, f(seed), f(f(seed)), etc.
Stream limit​(long maxSize)
Returns a stream consisting of the elements of this stream, truncated to be no longer than maxSize in length.
Stream map​(Function<? super T,​? extends R> mapper)
Returns a stream consisting of the results of applying the given function to the elements of this stream.
DoubleStream mapToDouble​(ToDoubleFunction<? super T> mapper)
Returns a DoubleStream consisting of the results of applying the given function to the elements of this stream.
IntStream mapToInt​(ToIntFunction<? super T> mapper)
Returns an IntStream consisting of the results of applying the given function to the elements of this stream.
LongStream mapToLong​(ToLongFunction<? super T> mapper)
Returns a LongStream consisting of the results of applying the given function to the elements of this stream.
Optional max​(Comparator<? super T> comparator)
Returns the maximum element of this stream according to the provided Comparator.
Optional min​(Comparator<? super T> comparator)
Returns the minimum element of this stream according to the provided Comparator.
boolean noneMatch​(Predicate<? super T> predicate)
Returns whether no elements of this stream match the provided predicate.
static Stream of​(T t)
Returns a sequential Stream containing a single element.
static Stream of​(T… values)
Returns a sequential ordered stream whose elements are the specified values.
static Stream ofNullable​(T t)
Returns a sequential Stream containing a single element, if non-null, otherwise returns an empty Stream.
Stream peek​(Consumer<? super T> action)
Returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream.
Optional reduce​(BinaryOperator accumulator)
Performs a reduction on the elements of this stream, using an associative accumulation function, and returns an Optional describing the reduced value, if any.
T reduce​(T identity, BinaryOperator accumulator)
Performs a reduction on the elements of this stream, using the provided identity value and an associative accumulation function, and returns the reduced value.
U reduce​(U identity, BiFunction<U,​? super T,​U> accumulator, BinaryOperator combiner)
Performs a reduction on the elements of this stream, using the provided identity, accumulation and combining functions.
Stream skip​(long n)
Returns a stream consisting of the remaining elements of this stream after discarding the first n elements of the stream.
Stream sorted()
Returns a stream consisting of the elements of this stream, sorted according to natural order.
Stream sorted​(Comparator<? super T> comparator)
Returns a stream consisting of the elements of this stream, sorted according to the provided Comparator.
default Stream takeWhile​(Predicate<? super T> predicate)
Returns, if this stream is ordered, a stream consisting of the longest prefix of elements taken from this stream that match the given predicate.
Object[] toArray()
Returns an array containing the elements of this stream.
A[] toArray​(IntFunction<A[]> generator)
Returns an array containing the elements of this stream, using the provided generator function to allocate the returned array, as well as any additional arrays that might be required for a partitioned execution or for resizing.

3. Collectors

averagingDouble
averagingInt
averagingLong
boxSupplier
castingIdentity
collectingAndThen
Collectors
computeFinalSum
counting
groupingBy
groupingByConcurrent
joining
mapMerger
mapping
maxBy
minBy
partitioningBy
reducing
summarizingDouble
summarizingInt
summarizingLong
summingDouble
summingInt
summingLong
sumWithCompensation
throwingMerger
toCollection
toConcurrentMap
toList
toMap
toSet

4. Resouce

1. 配置 aliyun 仓库

1.1. 1. 对当前项目起作用

在 build.gradle 中添加

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
buildscript {
repositories {
mavenLocal()
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter' }
mavenCentral()
}
}

allprojects {
repositories {
mavenLocal()
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter' }
mavenCentral()
}
}

1.2. 2. 对所有项目起作用

$HOME/.gradle/init.d 下创建 init.gradle 文件
(初始化文件相关见: https://docs.gradle.org/current/userguide/init_scripts.html)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
allprojects{
repositories {
def ALIYUN_REPOSITORY_URL = 'http://maven.aliyun.com/nexus/content/groups/public'
def ALIYUN_JCENTER_URL = 'http://maven.aliyun.com/nexus/content/repositories/jcenter'
all { ArtifactRepository repo ->
if(repo instanceof MavenArtifactRepository){
def url = repo.url.toString()
if (url.startsWith('https://repo1.maven.org/maven2')) {
project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_REPOSITORY_URL."
remove repo
}
if (url.startsWith('https://jcenter.bintray.com/')) {
project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_JCENTER_URL."
remove repo
}
}
}
maven {
url ALIYUN_REPOSITORY_URL
url ALIYUN_JCENTER_URL
}
}
}

2. buildScript 块的作用

buildScript 中配置的是给 build 脚本使用, 比如 task 中可以引用第三方的包; 而它之外的配置是给项目使用的.
buildScript 中也可以配置 repositories, plugin, dependency
gradle 执行脚本时, 优先执行 buildScript 中的内容, 然后才会执行剩余的脚本内容.

3. dependencies

api
implementation
compile
compileOnly
annotationProcessor
testCompile
testImplementation
testRuntimeOnly
testAnnotationProcessor
runtime

implementation(platform(“mavenBom”))
implementation(project(":module"))

4. 使用 Gradle 测试, 而不是 Junit 或 testNG

Settings / Build, Execution, Deployment / Build Tool / Gradle
Running test using -> Gradle

5. 命令

1
2
3
4
5
6
7

# 查看命令帮助文档
gradle -q help --task $task_name

# 将 maven 项目转化为 gradle
# --dsl=kotlin 表示转化成 kotlin-script 语法文件, 但是目前 **不支持**
gradle init --type pom --dsl=kotlin

6. 应用脚本插件

//脚本插件
apply from: ‘other.gradle’

7. 常见问题

7.1. 1. 下载依赖源码

https://stackoverflow.com/questions/11474729/how-to-build-sources-jar-with-gradle
https://docs.gradle.org/6.0/userguide/plugin_reference.html

7.2. 2. Deprecated Gradle features were used in this build, making it incompatible with Gradle

本地 gradle 版本过低, 调高默认版本
在 gradle/wrapper/gradle-wrapper.properties 设置 gradle 版本

1
2
3
4
5
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.0.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

7.3. 2. 脚本插件报找不到 sourceSets

https://stackoverflow.com/questions/52975515/unresolved-reference-sourcesets-for-gradle-kotlin-dsl

用 project.the() 替代脚本插件中的 sourceSets

8. Resource

https://docs.gradle.org/current/userguide/what_is_gradle.html#five_things
https://docs.gradle.org/current/userguide/java_library_plugin.html#sec:java_library_separation

Gradle 编译环境参数 Gradle 核心插件 插件查询 Gradle **插件使用** Gradle 插件介绍 Gradle 中文文档 Maven Publish Maven Publish 使用 kotlin-dsl-samples kotlin-examples Gradle 中文文档