diff --git a/.idea/gradle.xml b/.idea/gradle.xml index b1c23af..6a1ce52 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -12,8 +12,9 @@ diff --git a/API/build.gradle b/API/build.gradle index 58028cf..ff45d90 100644 --- a/API/build.gradle +++ b/API/build.gradle @@ -14,7 +14,7 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-webflux' - implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.1.0' + implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:3.0.0-M1' //implementation 'org.springframework.boot:spring-boot-starter-actuator' testImplementation 'org.springframework.boot:spring-boot-starter-test' diff --git a/Core/src/main/java/me/zacharias/chat/core/LaunchOptions.java b/Core/src/main/java/me/zacharias/chat/core/LaunchOptions.java index 02dc161..10bed7b 100644 --- a/Core/src/main/java/me/zacharias/chat/core/LaunchOptions.java +++ b/Core/src/main/java/me/zacharias/chat/core/LaunchOptions.java @@ -5,7 +5,7 @@ package me.zacharias.chat.core; */ public class LaunchOptions { private static LaunchOptions instance = new LaunchOptions(); - + /** * Gets the singleton instance of the LaunchOptions class. * Meant to be used to get or set ant option. diff --git a/Display/build.gradle b/Display/build.gradle index f3d6c25..2876ae5 100644 --- a/Display/build.gradle +++ b/Display/build.gradle @@ -11,6 +11,9 @@ dependencies { implementation project(":GeniusAPI") implementation project(":API") implementation project(":WikipediaTool") + + implementation("com.github.docker-java:docker-java-core:3.6.0") + implementation("com.github.docker-java:docker-java-transport-httpclient5:3.6.0") } test { diff --git a/Display/src/main/java/me/zacharias/chat/display/Display.java b/Display/src/main/java/me/zacharias/chat/display/Display.java index 638d8f5..19eb9e3 100644 --- a/Display/src/main/java/me/zacharias/chat/display/Display.java +++ b/Display/src/main/java/me/zacharias/chat/display/Display.java @@ -61,7 +61,7 @@ public class Display { core.addTool(new TimeTool(), Core.Source.INTERNAL); // TODO: Well Docker failes when luanched.... Fuck - // core.addTool(new PythonRunner(core), Core.Source.INTERNAL); + //core.addTool(new PythonRunner(core), Core.Source.INTERNAL); core.addTools(new MALAPITool().getOllamaTools()); core.addTools(new GeniusTools().getGeniusTools()); core.addTools(new WikipediaTool().getWikipediaToolsInstance()); diff --git a/Display/src/main/java/me/zacharias/chat/display/PythonRunner.java b/Display/src/main/java/me/zacharias/chat/display/PythonRunner.java index f99146f..a014539 100644 --- a/Display/src/main/java/me/zacharias/chat/display/PythonRunner.java +++ b/Display/src/main/java/me/zacharias/chat/display/PythonRunner.java @@ -8,19 +8,23 @@ import com.github.dockerjava.api.model.BuildResponseItem; import com.github.dockerjava.api.model.Frame; import com.github.dockerjava.api.model.Statistics; import com.github.dockerjava.core.DefaultDockerClientConfig; -import com.github.dockerjava.core.DockerClientBuilder; +import com.github.dockerjava.httpclient5.ApacheDockerHttpClient; +import com.github.dockerjava.transport.DockerHttpClient; import me.zacharias.chat.core.Core; import me.zacharias.chat.core.Pair; import me.zacharias.chat.ollama.*; import me.zacharias.chat.ollama.exceptions.OllamaToolErrorException; +import org.apache.commons.io.IOUtils; import org.json.JSONArray; import org.json.JSONObject; import java.io.*; import java.net.ServerSocket; import java.net.Socket; +import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; +import java.time.Duration; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -112,13 +116,48 @@ public class PythonRunner extends OllamaFunctionTool { e.printStackTrace(); } - DefaultDockerClientConfig.Builder config + DefaultDockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder() .withDockerHost("tcp://localhost:2375") - .withDockerTlsVerify(false); - dockerClient = DockerClientBuilder - .getInstance(config) + .withDockerTlsVerify(false) + .withDockerCertPath("~/.docker") .build(); + DockerHttpClient dockerHttpClient = new ApacheDockerHttpClient.Builder() + .dockerHost(config.getDockerHost()) + .maxConnections(10) + .connectionTimeout(Duration.ofSeconds(100)) + .responseTimeout(Duration.ofSeconds(100)) + .sslConfig(config.getSSLConfig()) + .build(); + + DockerHttpClient.Request ping = DockerHttpClient.Request.builder() + .method(DockerHttpClient.Request.Method.GET) + .path("/_ping") + .build(); + + try(DockerHttpClient.Response response = dockerHttpClient.execute(ping)) + { + if(!(response.getStatusCode() == 200)) + { + writeLog("Failed to ping docker"); + System.out.println("Failed to ping docker. Docker components is disabled."); + dockerClient = null; + return; + } + if(!(IOUtils.toString(response.getBody(), Charset.defaultCharset()).equals("OK"))) + { + writeLog("Failed to ping docker"); + System.out.println("Failed to ping docker. Docker components is disabled."); + dockerClient = null; + return; + } + } + catch (Exception e) + { + writeLog("Failed to ping docker"); + System.out.println("Failed to ping docker. Docker components is disabled."); + dockerClient = null; + } } @Override @@ -142,6 +181,10 @@ public class PythonRunner extends OllamaFunctionTool { @Override public OllamaToolRespnce function(OllamaFunctionArgument... args) { + if(dockerClient == null) + { + return new OllamaToolRespnce(name(), "Docker is disabled"); + } if(args.length == 0) { throw new OllamaToolErrorException(name(), "Missing code argument"); diff --git a/Display/src/main/java/org/apache/hc/client5/http/ssl/DefaultClientTlsStrategy.java b/Display/src/main/java/org/apache/hc/client5/http/ssl/DefaultClientTlsStrategy.java new file mode 100644 index 0000000..7a74db5 --- /dev/null +++ b/Display/src/main/java/org/apache/hc/client5/http/ssl/DefaultClientTlsStrategy.java @@ -0,0 +1,165 @@ +/* + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ + +package org.apache.hc.client5.http.ssl; + +import javax.net.ssl.HostnameVerifier; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLEngine; +import javax.net.ssl.SSLParameters; + +import org.apache.hc.core5.annotation.Contract; +import org.apache.hc.core5.annotation.ThreadingBehavior; +import org.apache.hc.core5.function.Factory; +import org.apache.hc.core5.http.nio.ssl.TlsStrategy; +import org.apache.hc.core5.reactor.ssl.SSLBufferMode; +import org.apache.hc.core5.reactor.ssl.TlsDetails; +import org.apache.hc.core5.ssl.SSLContexts; + +/** + * TLS upgrade strategy for non-blocking client connections. + * + * @since 5.0 + */ +@Contract(threading = ThreadingBehavior.STATELESS) +public class DefaultClientTlsStrategy extends AbstractClientTlsStrategy { + + /** + * @since 5.4 + */ + public static DefaultClientTlsStrategy createDefault() { + return new DefaultClientTlsStrategy( + SSLContexts.createDefault(), + HostnameVerificationPolicy.BOTH, + HttpsSupport.getDefaultHostnameVerifier()); + } + + /** + * @since 5.4 + */ + public static DefaultClientTlsStrategy createSystemDefault() { + return new DefaultClientTlsStrategy( + SSLContexts.createSystemDefault(), + HttpsSupport.getSystemProtocols(), + HttpsSupport.getSystemCipherSuits(), + SSLBufferMode.STATIC, + HostnameVerificationPolicy.BOTH, + HttpsSupport.getDefaultHostnameVerifier()); + } + + /** + * @deprecated Use {@link #createDefault()}. + */ + @Deprecated + public static TlsStrategy getDefault() { + return createDefault(); + } + + /** + * @deprecated Use {@link #createSystemDefault()}. + */ + @Deprecated + public static TlsStrategy getSystemDefault() { + return createSystemDefault(); + } + + /** + * @deprecated To be removed. + */ + @Deprecated + private Factory tlsDetailsFactory; + + /** + * @deprecated Use {@link DefaultClientTlsStrategy#DefaultClientTlsStrategy(SSLContext, String[], String[], SSLBufferMode, HostnameVerifier)} + */ + @Deprecated + public DefaultClientTlsStrategy( + final SSLContext sslContext, + final String[] supportedProtocols, + final String[] supportedCipherSuites, + final SSLBufferMode sslBufferManagement, + final HostnameVerifier hostnameVerifier, + final Factory tlsDetailsFactory) { + super(sslContext, supportedProtocols, supportedCipherSuites, sslBufferManagement, HostnameVerificationPolicy.CLIENT, hostnameVerifier); + this.tlsDetailsFactory = tlsDetailsFactory; + } + + /** + * @since 5.4 + */ + public DefaultClientTlsStrategy( + final SSLContext sslContext, + final String[] supportedProtocols, + final String[] supportedCipherSuites, + final SSLBufferMode sslBufferManagement, + final HostnameVerificationPolicy hostnameVerificationPolicy, + final HostnameVerifier hostnameVerifier) { + super(sslContext, supportedProtocols, supportedCipherSuites, sslBufferManagement, hostnameVerificationPolicy, hostnameVerifier); + } + + public DefaultClientTlsStrategy( + final SSLContext sslContext, + final String[] supportedProtocols, + final String[] supportedCipherSuites, + final SSLBufferMode sslBufferManagement, + final HostnameVerifier hostnameVerifier) { + this(sslContext, supportedProtocols, supportedCipherSuites, sslBufferManagement, HostnameVerificationPolicy.CLIENT, hostnameVerifier); + } + + public DefaultClientTlsStrategy( + final SSLContext sslContext, + final HostnameVerifier hostnameVerifier) { + this(sslContext, null, null, SSLBufferMode.STATIC, hostnameVerifier); + } + + /** + * @since 5.4 + */ + public DefaultClientTlsStrategy( + final SSLContext sslContext, + final HostnameVerificationPolicy hostnameVerificationPolicy, + final HostnameVerifier hostnameVerifier) { + this(sslContext, null, null, SSLBufferMode.STATIC, hostnameVerificationPolicy, hostnameVerifier); + } + + public DefaultClientTlsStrategy(final SSLContext sslContext) { + this(sslContext, HttpsSupport.getDefaultHostnameVerifier()); + } + + @Override + void applyParameters(final SSLEngine sslEngine, final SSLParameters sslParameters, final String[] appProtocols) { + sslParameters.setApplicationProtocols(appProtocols); + sslEngine.setSSLParameters(sslParameters); + } + + @Override + @SuppressWarnings("deprecated") + TlsDetails createTlsDetails(final SSLEngine sslEngine) { + return tlsDetailsFactory != null ? tlsDetailsFactory.create(sslEngine) : null; + } + +} diff --git a/Display/src/main/java/org/apache/hc/client5/http/ssl/HostnameVerificationPolicy.java b/Display/src/main/java/org/apache/hc/client5/http/ssl/HostnameVerificationPolicy.java new file mode 100644 index 0000000..d905c1f --- /dev/null +++ b/Display/src/main/java/org/apache/hc/client5/http/ssl/HostnameVerificationPolicy.java @@ -0,0 +1,26 @@ +package org.apache.hc.client5.http.ssl; + +/** + * Hostname verification policy. + * + * @see javax.net.ssl.HostnameVerifier + * @see DefaultHostnameVerifier + * + * @since 5.4 + */ +public enum HostnameVerificationPolicy { + + /** + * Hostname verification is delegated to the JSSE provider, usually executed during the TLS handshake. + */ + BUILTIN, + /** + * Hostname verification is executed by HttpClient post TLS handshake. + */ + CLIENT, + /** + * Hostname verification is executed by the JSSE provider and by HttpClient post TLS handshake. + */ + BOTH + +} \ No newline at end of file diff --git a/Display/src/main/java/org/apache/hc/client5/http/ssl/TlsSocketStrategy.java b/Display/src/main/java/org/apache/hc/client5/http/ssl/TlsSocketStrategy.java new file mode 100644 index 0000000..338f922 --- /dev/null +++ b/Display/src/main/java/org/apache/hc/client5/http/ssl/TlsSocketStrategy.java @@ -0,0 +1,31 @@ +package org.apache.hc.client5.http.ssl; + +import org.apache.hc.core5.annotation.Contract; +import org.apache.hc.core5.annotation.ThreadingBehavior; +import org.apache.hc.core5.http.protocol.HttpContext; + +import javax.net.ssl.SSLSocket; +import java.io.IOException; +import java.net.Socket; + +@Contract(threading = ThreadingBehavior.STATELESS) +public interface TlsSocketStrategy { + + /** + * Upgrades the given plain socket and executes the TLS handshake over it. + * + * @param socket the existing plain socket + * @param target the name of the target host. + * @param port the port to connect to on the target host. + * @param context the actual HTTP context. + * @param attachment connect request attachment. + * @return socket upgraded to TLS. + */ + SSLSocket upgrade( + Socket socket, + String target, + int port, + Object attachment, + HttpContext context) throws IOException; + +} \ No newline at end of file diff --git a/GeniusAPI/src/main/java/me/zacharias/neuro/dock/genius/GeniusTools.java b/GeniusAPI/src/main/java/me/zacharias/neuro/dock/genius/GeniusTools.java index 5847708..6094f80 100644 --- a/GeniusAPI/src/main/java/me/zacharias/neuro/dock/genius/GeniusTools.java +++ b/GeniusAPI/src/main/java/me/zacharias/neuro/dock/genius/GeniusTools.java @@ -1,6 +1,5 @@ package me.zacharias.neuro.dock.genius; -import com.google.common.reflect.ClassPath; import io.github.classgraph.ClassGraph; import io.github.classgraph.ClassInfo; import io.github.classgraph.ClassInfoList; diff --git a/build.gradle b/build.gradle index 0701c94..24a8b0b 100644 --- a/build.gradle +++ b/build.gradle @@ -22,7 +22,6 @@ subprojects { dependencies { implementation("org.json:json:20250107") - implementation("com.github.docker-java:docker-java:3.4.1") implementation("org.jetbrains:annotations:23.1.0") testImplementation 'org.junit.jupiter:junit-jupiter:5.10.0' diff --git a/launcher/src/main/java/me/zacharias/chat/launcher/Launcher.java b/launcher/src/main/java/me/zacharias/chat/launcher/Launcher.java index 038b6a6..df5ee42 100644 --- a/launcher/src/main/java/me/zacharias/chat/launcher/Launcher.java +++ b/launcher/src/main/java/me/zacharias/chat/launcher/Launcher.java @@ -47,7 +47,7 @@ public class Launcher { } } case "--api" -> { - System.out.println("API available at https://server.4zellen.se:3000/Zacharias/chat_thing/wiki/API-Docs"); + System.out.println("API available at https://git.server.4zellen.se/Zacharias/chat_thing/wiki/API-Docs"); return; } case "--help", "-h" -> {