Skip to content

Commit 4f7c768

Browse files
authored
Added proxy for YandexMusic source. (#299)
1 parent 578e0f1 commit 4f7c768

File tree

5 files changed

+92
-32
lines changed

5 files changed

+92
-32
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ plugins:
156156
playlistLoadLimit: 1 # The number of pages at 100 tracks each
157157
albumLoadLimit: 1 # The number of pages at 50 tracks each
158158
artistLoadLimit: 1 # The number of pages at 10 tracks each
159+
# proxy: # If defined, YandexMusic HTTP requests will be proxied through here. YandexMusic uses region blocking
160+
# url: "https://example.org" # The HTTP proxy to use
161+
# username: "my-bot" # Optional username to authenticate with the proxy
162+
# password: "youshallpass" # Optional password to authenticate with the proxy
159163
flowerytts:
160164
voice: "default voice" # (case-sensitive) get default voice from here https://api.flowery.pw/v1/tts/voices
161165
translate: false # whether to translate the text to the native language of voice

application.example.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ plugins:
5858
playlistLoadLimit: 1 # The number of pages at 100 tracks each
5959
albumLoadLimit: 1 # The number of pages at 50 tracks each
6060
artistLoadLimit: 1 # The number of pages at 10 tracks each
61+
# proxy: # If defined, YandexMusic HTTP requests will be proxied through here. YandexMusic uses region blocking
62+
# url: "https://example.org" # The HTTP proxy to use
63+
# username: "my-bot" # Optional username to authenticate with the proxy
64+
# password: "youshallpass" # Optional password to authenticate with the proxy
6165
flowerytts:
6266
voice: "default voice" # (case-sensitive) get default voice here https://flowery.pw/docs/flowery/tts-voices-v-1-tts-voices-get
6367
translate: false # whether to translate the text to the native language of voice

plugin/src/main/java/com/github/topi314/lavasrc/plugin/LavaSrcPlugin.java

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
import com.github.topi314.lavasrc.deezer.DeezerAudioSourceManager;
99
import com.github.topi314.lavasrc.deezer.DeezerAudioTrack;
1010
import com.github.topi314.lavasrc.flowerytts.FloweryTTSSourceManager;
11-
import com.github.topi314.lavasrc.lrclib.LrcLibLyricsManager;
1211
import com.github.topi314.lavasrc.jiosaavn.JioSaavnAudioSourceManager;
12+
import com.github.topi314.lavasrc.lrclib.LrcLibLyricsManager;
1313
import com.github.topi314.lavasrc.mirror.DefaultMirroringAudioTrackResolver;
1414
import com.github.topi314.lavasrc.plugin.config.*;
15+
import com.github.topi314.lavasrc.plugin.service.ProxyConfigurationService;
1516
import com.github.topi314.lavasrc.protocol.Config;
16-
import com.github.topi314.lavasrc.plugin.config.HttpProxyConfig;
1717
import com.github.topi314.lavasrc.qobuz.QobuzAudioSourceManager;
1818
import com.github.topi314.lavasrc.spotify.SpotifySourceManager;
1919
import com.github.topi314.lavasrc.tidal.TidalSourceManager;
@@ -23,11 +23,6 @@
2323
import com.github.topi314.lavasrc.ytdlp.YtdlpAudioSourceManager;
2424
import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager;
2525
import dev.arbjerg.lavalink.api.AudioPlayerManagerConfiguration;
26-
import org.apache.http.HttpHost;
27-
import org.apache.http.auth.AuthScope;
28-
import org.apache.http.auth.UsernamePasswordCredentials;
29-
import org.apache.http.impl.client.BasicCredentialsProvider;
30-
import dev.arbjerg.lavalink.api.AudioPlayerManagerConfiguration;
3126
import org.jetbrains.annotations.NotNull;
3227
import org.slf4j.Logger;
3328
import org.slf4j.LoggerFactory;
@@ -72,7 +67,8 @@ public LavaSrcPlugin(
7267
TidalConfig tidalConfig,
7368
QobuzConfig qobuzConfig,
7469
YtdlpConfig ytdlpConfig,
75-
JioSaavnConfig jioSaavnConfig
70+
JioSaavnConfig jioSaavnConfig,
71+
ProxyConfigurationService proxyConfigurationService
7672
) {
7773
log.info("Loading LavaSrc plugin...");
7874
this.sourcesConfig = sourcesConfig;
@@ -105,18 +101,26 @@ public LavaSrcPlugin(
105101
if (sourcesConfig.isDeezer() || lyricsSourcesConfig.isDeezer()) {
106102
this.deezer = new DeezerAudioSourceManager(deezerConfig.getMasterDecryptionKey(), deezerConfig.getArl(), deezerConfig.getFormats());
107103
}
104+
108105
if (sourcesConfig.isYandexMusic() || lyricsSourcesConfig.isYandexMusic()) {
109106
this.yandexMusic = new YandexMusicSourceManager(yandexMusicConfig.getAccessToken());
107+
108+
proxyConfigurationService.configure(this.yandexMusic, yandexMusicConfig.getProxy());
109+
110110
if (yandexMusicConfig.getPlaylistLoadLimit() > 0) {
111111
yandexMusic.setPlaylistLoadLimit(yandexMusicConfig.getPlaylistLoadLimit());
112112
}
113+
113114
if (yandexMusicConfig.getAlbumLoadLimit() > 0) {
114115
yandexMusic.setAlbumLoadLimit(yandexMusicConfig.getAlbumLoadLimit());
115116
}
117+
116118
if (yandexMusicConfig.getArtistLoadLimit() > 0) {
117119
yandexMusic.setArtistLoadLimit(yandexMusicConfig.getArtistLoadLimit());
118120
}
121+
119122
}
123+
120124
if (sourcesConfig.isFloweryTTS()) {
121125
this.flowerytts = new FloweryTTSSourceManager(floweryTTSConfig.getVoice());
122126
if (floweryTTSConfig.getTranslate()) {
@@ -172,30 +176,7 @@ public LavaSrcPlugin(
172176
if (sourcesConfig.isJiosaavn()) {
173177
this.jioSaavn = new JioSaavnAudioSourceManager(jioSaavnConfig.buildConfig());
174178

175-
HttpProxyConfig proxyConfig = jioSaavnConfig.getProxy();
176-
if (proxyConfig != null && proxyConfig.getUrl() != null) {
177-
HttpHost httpHost = HttpHost.create(proxyConfig.getUrl());
178-
179-
BasicCredentialsProvider credentialsProvider;
180-
if (proxyConfig.getUsername() != null && proxyConfig.getPassword() != null) {
181-
credentialsProvider = new BasicCredentialsProvider();
182-
credentialsProvider.setCredentials(
183-
new AuthScope(httpHost),
184-
new UsernamePasswordCredentials(proxyConfig.getUsername(), proxyConfig.getPassword())
185-
);
186-
} else {
187-
credentialsProvider = null;
188-
}
189-
190-
log.info("Using {} as http proxy for JioSaavn. With basic auth: {}", httpHost, credentialsProvider != null);
191-
192-
this.jioSaavn.configureBuilder(builder -> {
193-
builder.setProxy(httpHost);
194-
if (credentialsProvider != null) {
195-
builder.setDefaultCredentialsProvider(credentialsProvider);
196-
}
197-
});
198-
}
179+
proxyConfigurationService.configure(this.jioSaavn, jioSaavnConfig.getProxy());
199180
}
200181
}
201182

plugin/src/main/java/com/github/topi314/lavasrc/plugin/config/YandexMusicConfig.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
11
package com.github.topi314.lavasrc.plugin.config;
22

3+
import org.jetbrains.annotations.Nullable;
34
import org.springframework.boot.context.properties.ConfigurationProperties;
45
import org.springframework.stereotype.Component;
56

67
@ConfigurationProperties(prefix = "plugins.lavasrc.yandexmusic")
78
@Component
89
public class YandexMusicConfig {
910

11+
@Nullable
12+
private HttpProxyConfig proxy;
13+
1014
private String accessToken;
1115
private int playlistLoadLimit = 1;
1216
private int albumLoadLimit = 1;
1317
private int artistLoadLimit = 1;
1418

19+
@Nullable
20+
public HttpProxyConfig getProxy() {
21+
return this.proxy;
22+
}
23+
24+
@SuppressWarnings("unused")
25+
public void setProxy(@Nullable HttpProxyConfig proxy) {
26+
this.proxy = proxy;
27+
}
28+
1529
public String getAccessToken() {
1630
return this.accessToken;
1731
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.github.topi314.lavasrc.plugin.service;
2+
3+
import com.github.topi314.lavasrc.plugin.config.HttpProxyConfig;
4+
import com.sedmelluq.discord.lavaplayer.tools.io.HttpConfigurable;
5+
import org.apache.http.HttpHost;
6+
import org.apache.http.auth.AuthScope;
7+
import org.apache.http.auth.UsernamePasswordCredentials;
8+
import org.apache.http.impl.client.BasicCredentialsProvider;
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
11+
import org.springframework.stereotype.Service;
12+
13+
@Service
14+
public class ProxyConfigurationService {
15+
16+
private static final Logger log = LoggerFactory.getLogger(ProxyConfigurationService.class);
17+
18+
public void configure(HttpConfigurable httpConfigurable, HttpProxyConfig proxyConfig) {
19+
if (proxyConfig == null || proxyConfig.getUrl() == null) {
20+
return;
21+
}
22+
23+
HttpHost httpHost = HttpHost.create(proxyConfig.getUrl());
24+
BasicCredentialsProvider credentialsProvider = createCredentialsProvider(proxyConfig, httpHost);
25+
26+
log.info("Configuring HTTP proxy {} with authentication: {}", httpHost, credentialsProvider != null);
27+
28+
httpConfigurable.configureBuilder(builder -> {
29+
30+
builder.setProxy(httpHost);
31+
32+
if (credentialsProvider != null) {
33+
builder.setDefaultCredentialsProvider(credentialsProvider);
34+
}
35+
36+
});
37+
}
38+
39+
protected BasicCredentialsProvider createCredentialsProvider(HttpProxyConfig proxyConfig, HttpHost httpHost) {
40+
String username = proxyConfig.getUsername();
41+
String password = proxyConfig.getPassword();
42+
43+
if (username == null || password == null) {
44+
return null;
45+
}
46+
47+
BasicCredentialsProvider provider = new BasicCredentialsProvider();
48+
49+
provider.setCredentials(
50+
new AuthScope(httpHost),
51+
new UsernamePasswordCredentials(username, password)
52+
);
53+
54+
return provider;
55+
}
56+
57+
}

0 commit comments

Comments
 (0)